Yes. In Excel VBA, MsgBox displays an OK-only dialog by default, and you can make that choice explicit with vbOKOnly. For example, MsgBox "The report is ready.", vbOKOnly, "Done" shows the message with an OK button and a title.
What “OK only” means in VBA
MsgBox is a built-in VBA function for displaying a message in a dialog box. When the dialog contains only an OK button, it is a simple acknowledgement: the user reads the message and dismisses it before the macro continues. The dialog is modal, so VBA pauses at that line until it is dismissed.
The buttons are controlled by the optional buttons argument. If you leave that argument out, its default value is zero, which corresponds to vbOKOnly. Therefore, these two calls both display an OK-only box:
MsgBox "The update is complete."MsgBox "The update is complete.", vbOKOnly
Writing vbOKOnly explicitly can make code easier to understand, especially when you also add an icon, a title, or other button settings. It does not create a different kind of OK button; it simply states which button layout you intend.
MsgBox syntax and arguments
The general VBA syntax is MsgBox(prompt, [buttons], [title], [helpfile], [context]). The square brackets indicate optional arguments; do not type the brackets into your code. The prompt is the message shown to the user. The buttons argument can select a button group and, optionally, an icon, a default button, or a modality setting. The title supplies the text in the dialog’s title bar.
If you omit the title, VBA uses the host application name. In Excel, that will ordinarily identify Excel rather than provide a custom description of what the message concerns. A short, specific title such as “Import complete” can make the dialog easier to interpret.
The final two arguments, help file and context, relate to help integration and are rarely needed in ordinary workbook macros. Most calls use only the prompt, buttons, and title. You can also include a line break in a longer prompt with vbCrLf, the VBA constant for a carriage return and line feed.
Basic examples of an OK-only message
For a short notification, call MsgBox with the message alone. In this form, VBA uses the default OK-only button layout:
Option Explicit
Sub ShowNotice()
' Display a simple acknowledgement message.
MsgBox "The workbook update is complete."
End Sub
To spell out the button choice and set a title, use this version. Save a copy of the workbook before running a macro; macros can change workbook data, and their actions cannot be undone with Ctrl+Z.
Option Explicit
Sub ShowFinishedMessage()
' Use an explicit OK-only layout and a descriptive title.
MsgBox "The report is ready to review.", vbOKOnly, "Report complete"
End Sub
A message can also contain multiple lines. With the message text in the first argument and the button choice in the second, concatenate the lines using vbCrLf:
Option Explicit
Sub ShowImportSummary()
' Put each part of the summary on its own line.
MsgBox "Import finished." & vbCrLf & "Review the Results sheet.", _
vbOKOnly, "Import complete"
End Sub
The underscore at the end of the first continued line is VBA’s line-continuation character. It lets the statement continue on the next line; it is not part of the message displayed to the user.
Adding an icon without changing the buttons
An OK-only dialog does not have to be plain. You can combine the button setting with an icon constant. The usual icon constants are vbInformation, vbExclamation, vbQuestion, and vbCritical. For example:
Option Explicit
Sub ShowWarning()
' Keep one OK button and add a warning icon.
MsgBox "Some rows were skipped. Check the import log.", _
vbOKOnly Or vbExclamation, "Import warning"
End Sub
The Or operator combines the button and icon settings in the buttons argument. In this case, the user still sees only OK; the icon adds a visual cue, not another response. Choose an icon that matches the message. An informational icon suits a routine completion notice, while a warning or critical icon should be reserved for a genuine issue or serious condition.
Icons are cues rather than a replacement for clear wording. State what happened and, when useful, what the user should do next. For instance, “Some rows were skipped. Check the import log” is more actionable than a dialog that says only “Warning.”
When OK-only is the right choice
Use an OK-only message when the macro is informing the user of a result and does not need a choice before proceeding. Common uses include confirming that a report was created, explaining why an operation could not run, or telling someone where the macro placed its output. The message should normally describe a completed event or a condition the user needs to acknowledge.
Do not use OK-only if the macro’s next action depends on the user’s answer. For a real decision, select a button group that returns the required choice. For example, vbYesNo can ask whether to continue, while vbYesNoCancel includes a way to back out. A question mark icon alone does not create a Yes or No option; the button group determines the available responses.
For a potentially destructive action, make the prompt specific about what will happen and offer an appropriate cancellation path. You can also set the default button so that the safer option is initially selected. Do not rely on a dialog to replace a backup, validation, or clear explanation of the consequences.
Reading the result returned by MsgBox
MsgBox is a function, so it can return a value as well as display a dialog. When the only button is OK, the return value is vbOK. Capturing the result can be useful when you want to make the code’s intent explicit, although a one-button box offers no alternative button choice.
Option Explicit
Sub CheckAcknowledgement()
Dim response As VbMsgBoxResult
' An OK-only message returns vbOK when dismissed.
response = MsgBox("The checks are complete.", vbOKOnly, "Status")
If response = vbOK Then
Debug.Print "The message was acknowledged."
End If
End Sub
When the user must choose between actions, store the result in a variable declared as VbMsgBoxResult and compare it with named constants such as vbYes or vbNo. Named constants are clearer than comparing the result with unexplained numbers. In an OK-only box, checking the return value is usually unnecessary unless it helps the structure or readability of the procedure.
Common mistakes and practical limits
A frequent mistake is putting the title where the buttons argument belongs. The second argument is buttons, not title. To set a title while using OK only, include both arguments in order: MsgBox "Saved.", vbOKOnly, "Status". If you want to set a title but accept the default button layout, you still need the placeholder buttons argument: MsgBox "Saved.", , "Status".
- Wrong argument position: check the order of prompt, buttons, and title.
- Unclear prompt: explain the result or next step instead of showing a vague “Done.”
- Too many dialogs: a message box inside a loop can interrupt the user repeatedly. Consider collecting issues and reporting them once.
- Using a dialog for routine data: if the user needs to consult the information later, write it to a worksheet or log instead of relying only on a temporary pop-up.
A message box is a small dialog, not a full report window. Long instructions or large result lists are difficult to read there. Keep the prompt concise and move detailed output to a worksheet, a form, or another suitable part of the workbook. Because the dialog pauses execution, avoid showing one for every row processed in a large task.
Excel platform and macro considerations
Excel for Microsoft 365 on Windows supports VBA MsgBox in workbook macros. Excel for Mac also supports VBA, though the dialog’s appearance and some operating-system behavior can differ from Windows. The VBA call and the basic button constants are the same for this ordinary use. Excel for the web does not run VBA macros, so a workbook opened there cannot display a VBA message box from a macro.
VBA macros may be disabled by Excel’s security settings or an organization’s policy. If a macro does not run, first confirm that the file is in a macro-enabled format such as .xlsm and that the macro is permitted to run; do not weaken security settings simply to make an unknown file execute. A message box itself does not save the workbook or validate its contents. Make the macro’s data changes safely, and keep a backup before running code that edits or deletes information.
Frequently Asked Questions
Is OK the default button in VBA MsgBox?
Yes. If you omit the buttons argument, the default is vbOKOnly, so the dialog displays an OK button.
How do I make MsgBox show only OK?
Use MsgBox "Your message", vbOKOnly, "Your title". The title is optional; vbOKOnly makes the intended button layout explicit.
Can I add an icon to an OK-only MsgBox?
Yes. Combine vbOKOnly with an icon constant, such as vbOKOnly Or vbInformation. The icon does not add more buttons.
What does an OK-only MsgBox return?
It returns vbOK when the dialog is dismissed. Assign the function result to a VbMsgBoxResult variable if your code needs to refer to it.
Can I use VBA MsgBox in Excel for the web?
No. Excel for the web does not execute VBA macros. Use desktop Excel to run a VBA procedure that displays a message box.
Related
- Excel Data Validation Date Picker: Setup and Options
- Excel Data Validation for Dates: Rules and Examples
- Data Validation Checks in Excel: Rules and Examples
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Show UserForm: Open a Form in ExcelTo show an Excel VBA UserForm, call its Show method, usually from a standard module: UserForm1.Show. In Excel…
- Import Multiple Files Using VBA in Excel: A Practical GuideYes. In Excel for Microsoft 365 on Windows, VBA can loop through files in a selected folder, open…
- VBA Find Function: Examples, Options, and Common FixesIn Excel VBA, use the Range.Find method to search a range for a value or text. For example,…
- Excel VBA: Match a Trendline Label to Its Line ColorYes. In Excel VBA, set the trendline label’s font color to the trendline’s line color: use Trendline.DataLabel.Font.Color and…
- Excel VBA MsgBox New Line: Add Line Breaks CorrectlyTo put a new line in an Excel VBA message box, join the text with vbCrLf: MsgBox "First…
- Excel VBA Find and Replace: Methods and ExamplesTo find and replace text with Excel VBA, use the Range.Replace method and specify what to search for,…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.