Excel VBA MsgBox Yes/No with Timer
In this article
Excel VBA does not offer a native timed MsgBox, so you cannot make the built-in MsgBox close itself after a set number of seconds. The practical workaround is to build a small UserForm that mimics a Yes/No prompt and closes automatically when a timer expires, returning a default answer. This guide shows you how to create that form, wire up the timer, and handle the result in your macro. You will need Excel with the Developer tab enabled and basic familiarity with the VBA editor (Alt+F11).
What You'll Need
- Excel 2010 or later (Windows or Mac; the VBA editor is similar but Mac shortcuts differ slightly).
- Developer tab enabled: File > Options > Customize Ribbon > check Developer. On Mac: Excel > Preferences > Ribbon & Toolbar.
- Macro security set to allow macros: File > Options > Trust Center > Trust Center Settings > Macro Settings. Choose "Disable all macros with notification" so you can enable per file.
- A blank workbook or one where you can add a UserForm.
Step-by-Step
- Open the VBA editor. Press Alt+F11 (Windows) or Option+F11 (Mac). In the Project Explorer, right-click your workbook name and choose Insert > UserForm. A blank form appears.
- Design the form. In the Properties window (F4 if hidden), set the form's Name to
frmTimedYesNoand Caption to "Confirm". Drag a Label onto the form, name itlblMessage, and set its Caption to a placeholder like "Do you want to continue?". Add two CommandButtons: name thembtnYesandbtnNo, with Captions "Yes" and "No". Optionally add another Label namedlblCountdownto show remaining seconds. - Add the timer logic. Double-click the UserForm to open its code module. Declare a module-level variable at the top:
Dim endTime As Double. In the UserForm_Initialize event, setendTime = Now + TimeValue("00:00:10")for a 10-second timeout, and start the timer by callingApplication.OnTime endTime, "CloseTimedForm". Note: Application.OnTime schedules a macro at a specific time; you must have a standard module procedure named CloseTimedForm. - Create the close procedure. Insert a new standard module (Insert > Module) and add:
Public Sub CloseTimedForm()If UserForms.Count > 0 Then Unload frmTimedYesNoEnd Sub
This unloads the form when the timer fires. Because the form is unloaded without a button click, you need a way to record that the timeout occurred. - Track the result. In the UserForm code, declare
Public UserChoice As VbMsgBoxResult. In btnYes_Click setUserChoice = vbYesandUnload Me. In btnNo_Click setUserChoice = vbNoandUnload Me. In UserForm_QueryClose, if the form is closing because of the timer (you can check a flag), setUserChoice = vbNoas the default. A simpler approach: setUserChoice = vbNoin UserForm_Initialize before showing, so if the user does nothing the default is No. - Show the form from your macro. In a standard module, write:
Sub AskWithTimeout()Dim frm As frmTimedYesNoSet frm = New frmTimedYesNofrm.lblMessage.Caption = "Do you want to run the report?"frm.ShowIf frm.UserChoice = vbYes ThenMsgBox "You chose Yes"ElseMsgBox "You chose No or timed out"End IfEnd Sub
Run AskWithTimeout. The form appears with Yes/No buttons and closes after 10 seconds, returning No if untouched. - Cancel the timer if the user clicks. If the user clicks Yes or No before the timeout, you should cancel the scheduled OnTime call to avoid it firing later and trying to unload an already closed form. In both button click events, add
On Error Resume NextthenApplication.OnTime endTime, "CloseTimedForm", , False. The On Error handles the case where the scheduled time has already passed. - Test and adjust. Change the TimeValue string to control the timeout (e.g., "00:00:05" for 5 seconds). If you want a visible countdown, add a loop using
Application.Waitor aDoEventsloop in the form's Activate event, but keep it simple: updating lblCountdown every second requires a timer loop that can freeze Excel if not handled with DoEvents. For most uses, a static label saying "This will close in 10 seconds" is enough.
Common Mistakes
- Forgetting to cancel OnTime. If the user clicks a button and the scheduled OnTime still fires, VBA tries to unload a form that no longer exists, causing error 1004 or 361. Always cancel with the False argument.
- Using MsgBox and expecting a timer. The built-in MsgBox cannot be timed; any code that claims to do so is either using a UserForm or a Windows API call that is unreliable across Office versions.
- Placing the OnTime call in the wrong module. Application.OnTime must reference a public procedure in a standard module, not a UserForm module. If you put CloseTimedForm inside the form's code, the call fails silently.
- Not setting a default choice. If the form closes by timeout and UserChoice is never set, your macro may behave unpredictably. Set a default in Initialize.
- Ignoring macro security. If macros are disabled, the form never appears. Enable macros for the file or sign your project.
Tips
- Use a meaningful default: for destructive actions, default to No on timeout; for reminders, default to Yes.
- Keep the timeout short (5–15 seconds) so the macro does not hang waiting for input.
- Name your controls clearly (btnYes, btnNo, lblMessage) to make the code readable.
- If you need this in many workbooks, export the UserForm and module (File > Export File in the VBA editor) and import them elsewhere.
- Test on both Windows and Mac if your workbook is shared; OnTime behaviour is similar but timing precision can vary.
Troubleshooting
- The form does not close automatically. Check that CloseTimedForm is in a standard module and is Public. Verify the time string in TimeValue is valid (HH:MM:SS). If the scheduled time is in the past, OnTime fires immediately or not at all.
- Error 1004 "Unable to get the OnTime property". This usually means the procedure name is misspelled or the module was renamed. Ensure the name matches exactly.
- The form closes but the macro continues with the wrong choice. Confirm UserChoice is set before Unload Me in each button, and that the default is set in Initialize.
- Excel freezes while the form is open. Avoid infinite loops with DoEvents; if you add a countdown, include a small delay and DoEvents, and always provide an exit condition.
- Nothing happens when you run the macro. Check that macros are enabled and that you are running the correct procedure. Press Ctrl+G in the VBA editor to open the Immediate Window and test
AskWithTimeoutdirectly.
Frequently Asked Questions
Can I use the built-in MsgBox with a timer?
No. VBA's MsgBox function is modal and has no timeout parameter. You must use a UserForm or a Windows API hook, and the UserForm approach is the most reliable and portable.
Does this work on Excel for Mac?
Yes, the UserForm and Application.OnTime approach works on Mac Excel, though the VBA editor shortcuts differ (Option+F11 instead of Alt+F11). Timing precision may be slightly less exact.
How do I change the timeout duration?
Edit the TimeValue string in UserForm_Initialize. For example, TimeValue("00:00:05") gives 5 seconds. Update the label text to match if you show a message.
What happens if the user closes the form with the X button?
The form unloads without setting UserChoice unless you handle QueryClose. Add code in UserForm_QueryClose to set UserChoice to your default (e.g., vbNo) when the CloseMode is vbFormControlMenu.
Can I show a countdown on the form?
Yes, but it requires a loop with DoEvents and Application.Wait, which can make Excel less responsive. For simple prompts, a static "closes in 10 seconds" label is safer.
Why does my OnTime call fire twice?
If you schedule OnTime more than once without cancelling, multiple calls queue up. Always cancel the previous schedule before setting a new one, or use a flag to ignore duplicate firings.