Yes. To display a VBA variable in a message box, join it to a text string with the ampersand operator: MsgBox "The total is " & total. The variable’s current value appears in the message; you can also format it, put it on a new line, or combine it with other values.
How MsgBox displays a variable
MsgBox is a built-in VBA function that shows a dialog box. Its first argument, called the prompt, can be a string expression. That expression may be a literal message, a variable, or a combination of text and values. For example, if total contains 42, the expression "The total is " & total forms the prompt “The total is 42.”
The ampersand operator (&) joins pieces into one string. Put text in straight double quotation marks, place an ampersand between each piece, and write the variable name without quotation marks. If you put the variable name inside quotes, VBA treats it as literal text rather than looking up its value. For example, MsgBox "total" displays the word “total,” while MsgBox total displays the value stored in the variable.
VBA allows a call without parentheses when you are not using the function’s return value, as in MsgBox "Hello". With an expression such as a joined string, parentheses are commonly used for clarity: MsgBox ("Total: " & total). The parentheses are not what make the variable display; the concatenation expression does.
A complete Excel VBA example
Here is a small macro that assigns a value and shows it. It uses a declared variable, joins that value to a label, and gives the dialog a title. Before running any macro, save a copy of the workbook: VBA macros cannot be undone with Ctrl+Z.
Sub ShowTotal()
' Store a sample amount and display it.
Dim total As Double
total = 42.5
MsgBox "The total is " & total, vbOKOnly, "Summary"
End Sub
In Excel for Microsoft 365 on Windows, open the Visual Basic Editor with Alt+F11, choose Insert > Module, and paste the macro into the module. You can run it with F5 while the cursor is inside the procedure, or use the Run Sub/UserForm command in the editor. On a Mac, the Visual Basic Editor is available from Tools > Macro > Visual Basic Editor; the keyboard shortcut can depend on the keyboard’s function-key settings, so using the menu is a reliable option. The VBA code itself works in supported desktop versions of Excel on Windows and Mac.
The message box has three arguments in this example: the prompt, the buttons setting, and the title. The buttons argument is optional; vbOKOnly makes the intended button choice explicit. If you leave it out, VBA uses the default OK button. The title argument changes the text in the dialog’s title bar, not the prompt.
Display several variables or add line breaks
You can join more than one variable and separate the values with labels or punctuation. This is useful when a quick check needs to show a record’s key fields together. Add vbCrLf where you want a new line in the prompt:
Sub ShowOrderDetails()
' Display related values on separate lines.
Dim orderNumber As Long
Dim customerName As String
Dim quantity As Long
orderNumber = 1052
customerName = "Avery Chen"
quantity = 3
MsgBox "Order: " & orderNumber & vbCrLf & _
"Customer: " & customerName & vbCrLf & _
"Quantity: " & quantity, vbOKOnly, "Order details"
End Sub
The underscore at the end of a line is VBA’s line-continuation character, letting a statement continue on the next line. The space before it matters. You can also use vbNewLine as a line-break constant; vbCrLf is a common choice in Windows VBA. For a short message, a single line is easier to scan; for several fields, line breaks make the values less likely to run together.
Format numbers, dates, and currency
A value can be displayed as stored, but a raw number may not look like the value a person expects. Use VBA’s Format function to create a readable string before joining it to the prompt. For example, the format pattern "0.00" shows two decimal places. A date can be formatted with a pattern such as "dd-mmm-yyyy". The result of Format is text suitable for the message.
Sub ShowFormattedAmount()
' Format the number for display in the prompt.
Dim amount As Double
amount = 1234.5
MsgBox "Amount: " & Format(amount, "0.00"), vbOKOnly, "Value"
End Sub
With the sample value, this displays Amount: 1234.50. The pattern deliberately does not add a currency symbol, because the appropriate currency and separators depend on the workbook’s purpose and the reader’s locale. If you use a literal symbol or a specific date pattern, make sure it matches what the audience should see. For a value that should follow the computer’s regional currency settings, the named format "Currency" can be used with Format, but the symbol and appearance can vary by system settings.
For dates, choose an unambiguous display when readers may use different regional conventions. For instance, a numeric date such as 04/05/2026 can mean April 5 or May 4 depending on locale. A month name in a pattern such as "d mmmm yyyy" is usually clearer. Formatting changes only the displayed string; it does not change the value stored in the variable.
Show a variable in a question and use the answer
A message box can also ask a question, and its return value tells the macro which button the user selected. This is different from displaying a variable: the prompt shows information, while the result of MsgBox is a value such as vbYes or vbNo. Store that result in a variable if the next step depends on the choice.
Sub ConfirmOrder()
' Save a copy of the workbook before running a macro.
Dim orderNumber As Long
Dim answer As VbMsgBoxResult
orderNumber = 1052
answer = MsgBox("Process order " & orderNumber & "?", _
vbYesNo + vbQuestion, "Confirm")
If answer = vbYes Then
MsgBox "Order " & orderNumber & " was selected."
Else
MsgBox "No changes were requested."
End If
End Sub
Here vbYesNo + vbQuestion requests Yes and No buttons and a question icon. The variable answer is declared as VbMsgBoxResult, the VBA type used for a message-box response. Always compare the result with a named constant such as vbYes, rather than relying on a numeric value. The example only displays a follow-up message; it does not modify an order or worksheet.
Common problems and how to fix them
If the box displays a variable name instead of its value, check whether that name was accidentally enclosed in quotation marks. If the output shows text joined without a space, add a space inside the text literal, such as "Total: ". A message like “Total:42” is usually a spacing issue, not a problem with the variable.
- “Variable not defined” or a compile error: Check spelling and scope. With
Option Explicitenabled, every variable must be declared, and a misspelled name is caught before the macro runs. - The variable shows an unexpected value: Check where it is assigned and whether that assignment runs before the
MsgBoxline. A message box displays the value at the moment that line executes. - A number has too many decimals: Format it first with
Format, choosing a pattern that fits the data. - The prompt appears blank or strange: Confirm that the variable contains a value of the expected type. Also check that text and variables are joined with
&, not added with the numeric+operator. - A line break is missing: Put
vbCrLfbetween the parts of the prompt and join it with ampersands.
For debugging, temporarily display the value immediately after the code that calculates it. This can reveal whether the issue is in the calculation or merely in the final message. You can also use Debug.Print to send a value to the Immediate window in the Visual Basic Editor, which is more convenient when repeatedly checking values and less disruptive than opening a dialog each time.
When MsgBox is the right choice—and alternatives
MsgBox works well for a short notification, a simple confirmation, or a quick one-time diagnostic. It pauses the macro until the user dismisses the dialog or chooses a button, so it can interrupt a workflow. It is not a good way to display a long report, a large table, or many messages in a loop; repeated dialogs force the user to respond one at a time.
Choose a different destination when the information should remain visible or be reviewed later:
- Worksheet cell: Write the result to a cell when it belongs with the workbook’s data or should be copied and printed.
- Immediate window: Use
Debug.Printfor temporary diagnostic output while developing a macro. - Status bar: Use Excel’s status bar for brief progress information that should not interrupt the user.
- UserForm: Build a custom form when the task needs several fields, instructions, or controls beyond the standard message-box choices.
These options serve different purposes: a message box asks for attention, while a worksheet or form can present more detail. Do not use a message box as the only record of an important result if the user may need to retrieve it after closing the dialog.
Frequently Asked Questions
How do I display a variable in a VBA MsgBox?
Join the variable to a text string with &, as in MsgBox "Name: " & customerName. Keep the variable name outside quotation marks so VBA reads its value.
Why does my MsgBox show the variable name instead of its value?
The name is probably inside quotation marks. MsgBox "amount" displays the literal word “amount”; MsgBox amount displays the contents of the variable.
Can I display a number and text in the same MsgBox?
Yes. Join the text and number with &, for example MsgBox "Items: " & itemCount. Use Format first if the number needs a particular number of decimal places or date style.
How do I put a variable on a new line in a MsgBox?
Join the prompt parts with vbCrLf, such as MsgBox "Name: " & personName & vbCrLf & "Age: " & age. Each label and value then appears on its own line.
Can MsgBox return the button the user clicked?
Yes. Assign the function result to a variable declared as VbMsgBoxResult, then compare it with constants such as vbYes or vbNo. Set the available buttons with a constant such as vbYesNo.
Related
- Data Validation Checklist for Excel: What to Check
- Conditional Formatting in Microsoft Project: What Works
- Data Validation and Verification in Excel Explained
Try it: the Column Letter to Number: aA is 27, XFD is 16,384. Converts both ways.
More in VBA & macros
- VBA Range.Delete: Remove Cells, Rows, or ColumnsIn Excel VBA, use the Delete method to remove cells, rows, or columns: for example, Range("B2:C5").Delete Shift:=xlShiftUp deletes…
- VBA PivotTable Field Name Is Not Valid: Causes and FixesThe “PivotTable field name is not valid” message usually means Excel cannot read a proper heading for every…
- Outlook VBA: Select an Excel Worksheet by PathIn Outlook VBA on Windows, create an Excel application object, open the workbook by its full file path,…
- Excel Macro to Send Email: Setup, Code, and SafetyYes. In Excel for Microsoft 365 on Windows, a VBA macro can create an email in classic Outlook…
- VBA Function Syntax: How to Write and Call FunctionsIn VBA, a function starts with a Function declaration, contains statements that calculate a result, and ends with…
- Excel VBA Copy and Paste Formatting: Code and TipsTo copy only formatting with Excel VBA, copy the source range and use PasteSpecial with xlPasteFormats: sourceRange.Copy, then…
Sources: Function behaviour and menu paths: Microsoft Support documentation for Excel. Method & sources
Spotted an error in this guide? Let us know.