Excel VBA MsgBox OK: Step-by-Step Guide
In this article
To show a message box with just an OK button in Excel VBA, use MsgBox "Your message here". That single line displays a dialog with the text and a single OK button, and the code pauses until the user clicks it. If you want to control the button set explicitly, use MsgBox "Your message here", vbOKOnly. Both do the same thing, because vbOKOnly is the default button configuration. You do not need any add-in, reference, or special setting: the MsgBox function is built into VBA and works in every desktop version of Excel.
What You'll Need
- A desktop copy of Excel (Windows or Mac). MsgBox does not run in Excel for the web, because the web version has no VBA editor.
- A workbook saved as .xlsm (macro-enabled) if you want your code to persist. A normal .xlsx file will drop the macro when saved.
- The Developer tab visible, or another way to open the VBA editor. On Windows you can also press Alt+F11; on Mac, use Tools > Macro > Visual Basic Editor from the Excel menu.
- Macro security set to allow macros. If macros are disabled, your code will not run at all.
Step-by-Step
- Open the VBA editor. In Excel, press Alt+F11 on Windows, or on Mac choose Tools > Macro > Visual Basic Editor. Menu labels can differ slightly by Excel version, so if you do not see the exact item, look under the Macro submenu.
- Insert a module. In the editor, choose Insert > Module. A blank code window appears on the right. Code placed in a module can be run directly; code placed in a sheet module only runs in response to events on that sheet.
- Type the simplest version first. Enter this in the module:
Sub ShowMessage() MsgBox "Report is ready." End SubThis shows the text with a single OK button and a title bar reading Microsoft Excel. - Run it. Click anywhere inside the subroutine and press F5, or choose Run > Run Sub/UserForm. The message box appears. Click OK and the subroutine ends.
- Add a custom title. The third argument of MsgBox is the title bar text:
MsgBox "Report is ready.", vbOKOnly, "My Macro"
The second argument is the button and icon set.vbOKOnlyis the value 0, so you can also writeMsgBox "Report is ready.", 0, "My Macro". Named constants are easier to read and are the recommended style. - Add an icon if you want one. Combine the button constant with an icon constant using a plus sign:
vbOKOnly + vbInformation,vbOKOnly + vbExclamation, orvbOKOnly + vbCritical. The icon does not change the behaviour, only the appearance. - Capture the return value if you need it. MsgBox returns a number. With only an OK button the only possible return is
vbOK(value 1), so capturing it is rarely useful. It becomes useful when you switch to Yes/No or OK/Cancel, where you test the result:Dim answer As Integer answer = MsgBox("Continue?", vbOKCancel) If answer = vbOK Then ' user clicked OK End If - Build the message from cell values. Concatenate with an ampersand:
MsgBox "Total is " & Range("A1").Value. If the cell might be empty or hold an error, wrap it:MsgBox "Total is " & CStr(Range("A1").Text). The .Text property returns what is displayed, which avoids type mismatch errors. - Add line breaks. Use
vbCrLforvbNewLineinside the string:MsgBox "Line one" & vbCrLf & "Line two". Long single-line messages get clipped on narrow screens, so break them up. - Save the workbook as macro-enabled. Use File > Save As and pick Excel Macro-Enabled Workbook (.xlsm). Then test again after reopening, to confirm the macro survived the save.
Common Mistakes
- Forgetting the quotes.
MsgBox Hellofails because VBA reads Hello as a variable name. Text must be in double quotes; variables must not be. - Using straight quotes from a word processor. If you paste code from a document that converted quotes to curly ones, VBA throws a syntax error. Retype the quotes in the editor.
- Putting the title in the second argument. The order is message, buttons, title. Swapping buttons and title produces a type mismatch or an unexpected dialog.
- Combining constants incorrectly. Use
+(orOr) between one button constant and one icon constant. Adding two button constants, such asvbOKOnly + vbYesNo, gives a meaningless number. - Expecting MsgBox to return text. It returns an integer code, never the label the user clicked. Compare against the named constants instead.
- Saving as .xlsx. Excel warns you that macros cannot be saved, and if you accept, the code is gone next time.
- Running the wrong subroutine. With the cursor outside any procedure, F5 prompts you to choose one. Click inside the Sub first.
Tips
- Keep messages short and specific. "Done" tells the user nothing; "Copied 42 rows to Sheet2" does.
- Use
vbInformationfor confirmations,vbExclamationfor warnings, andvbCriticalonly for genuine failures, so users learn to read the icon. - Do not put a MsgBox inside a loop that runs hundreds of times. It forces a click for every iteration. Collect results and show one summary at the end.
- For anything the user must read and act on later, write to a cell or a log sheet instead of a dialog.
- Use
vbOKOnlywhen you are informing, and OK/Cancel or Yes/No when you are asking. A dialog with only OK that asks a question is a dead end. - Test with the VBA editor's Immediate window using
?MsgBox("test")for quick checks without writing a full procedure.
Troubleshooting
The macro will not run at all. Check the macro security setting under File > Options > Trust Center > Trust Center Settings > Macro Settings. Also confirm the file is .xlsm. If the workbook came from another source, Windows may have blocked it; open the file's properties in File Explorer and look for an unblock option.
Compile error: Expected: end of statement. Almost always a missing or mismatched quote, or a stray parenthesis. Count the quotes in the line.
Compile error: Variable not defined. You typed text without quotes, or Option Explicit is on and you used a variable you never declared with Dim.
The dialog appears behind Excel. This happens when the code runs from another application or during certain automation. Bring Excel to the front, or avoid showing dialogs from unattended code.
The message is cut off. MsgBox does not scroll. Insert vbCrLf breaks or shorten the text; very long messages are better written to a cell.
Type mismatch on a cell value. The cell holds an error value. Test with IsError before concatenating, or use the .Text property.
Nothing happens when you press F5. The cursor may be in a different procedure, or the module is a sheet or ThisWorkbook module waiting for an event. Put the code in a standard module.
Frequently Asked Questions
What is the difference between MsgBox and MsgBox with vbOKOnly?
Nothing in practice. vbOKOnly is the default button setting, so the explicit constant only makes your intent clearer in the code.
Can I change the OK button text?
No. VBA's MsgBox uses fixed system button labels. If you need custom button text, you must build a small UserForm with your own buttons.
Why does my message box have a title of Microsoft Excel?
Because you left out the third argument. Supply a title string to replace it.
Does MsgBox work in Excel for the web or on mobile?
No. VBA is not available in Excel for the web or in the mobile apps, so the code only runs in the desktop application.
How do I show a number without decimals?
Format it before concatenating, for example with Format(value, "0"), or read the cell's .Text property.
Can a MsgBox close itself after a few seconds?
Not with the built-in function. It waits for a click. A UserForm with a timer is the usual alternative, but it adds complexity and is rarely worth it.