January 28, 2020

Automatically Create Message Box with Timer

How to create a message box automatically that closes itself after a specific time using VBA.
Here’s the complete VBA code:
Private Sub Workbook_Open()
End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)
End Sub

Option Explicit

Const PopupDurationSecs As Integer = 5

Sub startTimer()
Application.OnTime Now + TimeValue(“00:00:05”), “myShellMessageBox”
End Sub
Sub myShellMessageBox()

Dim Result As Integer

Result = CreateObject(“WScript.Shell”).PopUp( _
“Keep this workbook open?”, PopupDurationSecs, _
“Keep Workbook Open”, 4 + 32)
‘MsgBox Result
If Result = 6 Then
Exit Sub
ElseIf Result = 7 Then
End If
End Sub

For more on this method see the MSDN Documentation