MsgBox for Beginners: Show Messages and Get Answers When writing macros in Excel VBA, you often need to talk to your user. The simplest way to do this is with a message box. A message box displays a small pop-up window on the screen. It can show information, warn about an error, or ask the user a question before running more code.
Here is how to master the MsgBox function, from your very first popup to collecting user decisions. 1. The Simplest Message Box (Just Text)
If you only want to display a basic informational note, you only need to provide a single piece of text inside quotation marks.
Sub ShowBasicMessage() MsgBox “Hello! This is a simple message box.” End Sub Use code with caution.
When this code runs, Excel pauses, shows the text, and provides an OK button. The code starts running again only after the user clicks OK. 2. Upgrading Your Box with Titles and Icons
A plain message box can look boring. You can make it look professional by adding an icon and a custom title bar.
The full structure looks like this:MsgBox(Prompt, Buttons, Title) Prompt: The text inside the box.
Buttons: The icon style and button choices (like OK or Yes/No). Title: The text at the very top of the window.
Sub ShowBetterMessage() MsgBox “Your data has been successfully saved!”, vbInformation, “System Update” End Sub Use code with caution. Common Icon Options: vbInformation – Shows a blue “i” icon for general notes.
vbExclamation – Shows a yellow warning triangle for important alerts. vbCritical – Shows a red “X” for serious errors. vbQuestion – Shows a blue question mark for inquiries. 3. Asking Questions and Getting Answers
Sometimes you do not just want to talk to the user; you want to get an answer from them. For example, you might want to ask, “Do you want to delete this sheet?” To capture their answer, you must do two things: Wrap the MsgBox arguments in parentheses. Store the answer in a variable.
Sub GetUserAnswer() Dim UserResponse As VbMsgBoxResult ‘ Ask the question and show Yes/No buttons UserResponse = MsgBox(“Do you want to clear the entire report?”, vbYesNo + vbQuestion, “Clear Data”) ’ Check what the user clicked If UserResponse = vbYes Then MsgBox “Clearing data now…”, vbInformation, “Action Confirmed” ‘ Put your data clearing code here Else MsgBox “Action canceled.”, vbExclamation, “Action Stopped” ’ Put your backup plan here End If End Sub Use code with caution. Understanding the Response Codes:
When you use vbYesNo, VBA checks the variable against these built-in terms: vbYes – The user clicked Yes. vbNo – The user clicked No.
You can also use vbOKCancel or vbRetryCancel if those buttons fit your project better. 4. Advanced Trick: Adding Line Breaks
If your message is long, it will look crowded on a single line. You can force text onto a new line by using vbNewLine and the & symbol to glue your text together.
Sub MultiLineMessage() MsgBox “Line 1: Task completed!” & vbNewLine & _ “Line 2: 50 rows were updated.” & vbNewLine & _ “Line 3: Click OK to finish.”, vbInformation, “Status Report” End Sub Use code with caution. Summary Checklist for Beginners Use MsgBox “text” for simple notifications.
Always use parentheses () when saving a user’s answer to a variable.
Combine buttons and icons using the plus sign (e.g., vbYesNo + vbQuestion).
Keep your text clear, short, and easy for the user to understand. If you want to customize your popup further, let me know:
What specific project or task you are building this macro for
Which button options you want to display (Yes/No, OK/Cancel, Retry)
If you need help writing the code that runs after the user clicks a button
I can provide the exact code block you need to drop into your project.
Leave a Reply