VBA MsgBox Auto Close: Options and Limitations

VBA & macros · Updated November 18, 2026

A standard VBA MsgBox cannot be set to close itself after a number of seconds: its arguments include no timeout option. On Windows, use the similar WScript.Shell.Popup method for a timed message, or create a modeless UserForm when you need more control.

What the built-in MsgBox can and cannot do

VBA’s MsgBox function displays a dialog and waits for the user to choose a button. Its optional arguments let you set the message, buttons, icon, title, help file, and help context, but not a duration. For example, MsgBox "The report is ready.", vbInformation, "Complete" remains on screen until someone responds.

This behavior is intentional: a message box is a synchronous, modal dialog. While it is open, the VBA procedure that called it waits. Adding a delay before the call does not make the dialog time out, and using Application.OnTime to schedule another macro does not let that macro interrupt a modal MsgBox. The waiting procedure must regain control before the scheduled code can do its work.

So if the question is whether you can add a timeout parameter to the normal MsgBox call, the answer is no. A timed alternative may look nearly identical, but it is a different method with different availability and return behavior.

Use WScript.Shell.Popup on Windows

For a short notice in Excel on Windows, WScript.Shell.Popup is a practical alternative. Its second argument is a timeout in seconds. If the user does not respond before the time expires, the dialog closes and the method returns -1. If a button is clicked, it returns the value associated with that button.

This example waits up to five seconds and uses an information icon with the default OK button:

Public Sub ShowTimedNotice()
    Dim shell As Object
    Dim result As Long

    ' Create the Windows Script Host shell object.
    Set shell = CreateObject("WScript.Shell")

    ' Show the notice for at most five seconds.
    result = shell.Popup("The report is ready.", 5, "Complete", vbInformation)

    If result = -1 Then
        Debug.Print "The notice timed out."
    ElseIf result = 1 Then
        Debug.Print "The user clicked OK."
    End If

    Set shell = Nothing
End Sub

Save a copy of the workbook before running a macro. VBA changes cannot be undone with Ctrl+Z. The example uses late binding, so it does not require adding a Windows Script Host reference in the VBA editor. It is for desktop Office on Windows; Windows Script Host may be restricted by an organization’s security policy.

Choose a timeout and interpret the result

The timeout is measured in seconds, not milliseconds. A value of five means the dialog will wait up to five seconds, but it may disappear sooner if a user clicks a button. A zero timeout means the dialog waits indefinitely, which removes the auto-close behavior. Avoid choosing a very short duration for a message that the user must read or act on.

With the example’s default OK button, a return value of 1 means OK was selected, while -1 means the timeout elapsed. If you add buttons, check the returned value against the relevant VBA constants rather than treating every non-timeout result as the same response. For example, a Yes/No popup can distinguish the user’s choice from an automatic timeout.

Timeouts are most suitable for nonessential status messages: a notice that a refresh finished, a brief confirmation that a background step completed, or a reminder that does not require a decision. If the user must make a choice before the macro continues, a timed close can create confusion or cause the user to miss the prompt. In that case, keep the dialog open or redesign the workflow so the choice is recorded elsewhere.

How it compares with MsgBox

Popup can resemble a message box, but it is not the VBA MsgBox function. It is a method provided by Windows Script Host, so it is tied to Windows and can be unavailable where that component is disabled. Its appearance can also vary with Windows and Office environments. Code that relies on its return values should explicitly handle both a button click and a timeout.

Another option sometimes mentioned is the Windows API function MessageBoxTimeout. It can display a Windows message box with a timeout, but it is not a normal VBA feature and is not documented as a stable public Windows API for general application use. API declarations also have 32-bit versus 64-bit compatibility concerns. For ordinary workbook automation, WScript.Shell.Popup is easier to maintain; use a custom form if the dialog needs richer behavior.

Neither method turns the built-in MsgBox into a timed one. Replacing the call is the key distinction: code such as MsgBox "Done", vbInformation, "Status", 5 is not a valid way to specify a five-second timeout because the fourth and fifth optional arguments relate to help information, not timing.

When a UserForm is a better choice

A VBA UserForm is preferable when the notice needs custom text, controls, branding, progress information, or a nonblocking layout. Show the form modelessly so the calling macro or user interface is not held in the same way as by a modal message box, then arrange for it to close after a delay. In Excel desktop, Application.OnTime can schedule a public procedure in a standard module to close a form.

This approach takes more setup than a popup. The scheduled procedure must be in a standard module, the form must be available when that procedure runs, and you should account for the user closing the form early. Excel may also run a scheduled procedure later than expected if it is busy, in cell-edit mode, or otherwise unable to run it at the scheduled time. It is therefore a way to build a timed interface, not a precise real-time timer.

A UserForm is also a better fit when automatically dismissing the notice should not be the only way to discover information. For instance, a completion message could be written to a worksheet or status area as well as shown briefly. That leaves a record after the form disappears and is more accessible to someone who was away from the screen.

Platform and compatibility limits

The built-in VBA MsgBox has no timeout parameter in desktop Excel for Windows or Mac. The Windows Script Host popup example is Windows-specific and should not be expected to work in Excel for Mac. On Mac, consider a UserForm and a scheduled close, then check the behavior in the Office version and workflow you support. Excel for the web does not run VBA macros, so neither VBA approach applies there.

Also consider your deployment environment. A macro can be blocked by Office macro security settings, and an organization can disable Windows Script Host. If a workbook is distributed to other people, test the chosen approach on a representative machine and provide a fallback message or a worksheet-based status. Avoid assuming that code working on one Windows computer will work unchanged on every Office platform.

Common mistakes and troubleshooting

  • Adding a number to MsgBox: There is no timeout argument, so a number placed in a later optional argument will not set a close delay.
  • Expecting OnTime to dismiss a modal box: The original VBA call is still waiting. Use a timed popup or a nonmodal form instead.
  • Ignoring the timeout return: Check for -1 so your macro can distinguish an automatic close from a button click.
  • Getting an automation error: Windows Script Host may be unavailable or restricted. Check organizational policy and use a UserForm fallback if necessary.
  • Assuming the popup is cross-platform: WScript.Shell is a Windows component. Do not use it as a Mac-compatible solution.

When troubleshooting, first confirm which dialog your code actually calls. If it uses MsgBox, no timeout is being applied. If it uses Popup, verify that the timeout argument is a number of seconds and that the procedure handles -1. If timing is important, remember that modal dialogs, workbook state, and security policy all affect what the user experiences.

Frequently Asked Questions

Can I make VBA MsgBox close after five seconds?
No. The standard function has no timeout argument. On Windows, use WScript.Shell.Popup with a timeout value of five, or create a timed UserForm.

What does Popup return when it times out?
WScript.Shell.Popup returns -1 when the timeout expires. A clicked button returns its button result, so handle these cases separately.

Does Application.OnTime close an open MsgBox?
No. A modal MsgBox keeps its calling VBA procedure waiting, so a scheduled macro cannot dismiss that dialog. Use a different dialog method.

Does WScript.Shell.Popup work in Excel for Mac?
No. It depends on Windows Script Host. Use a UserForm-based approach for a desktop Mac workflow, and verify it in the Office version you need to support.

Can Excel for the web run an auto-closing VBA message?
No. Excel for the web does not run VBA macros. A VBA popup or UserForm requires desktop Excel.

Related

Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.

More in VBA & macros

Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources

Spotted an error in this guide? Let us know.