Excel VBA: Write Array Values to a MsgBox
In this article
To show an array's values in a message box in Excel VBA, loop through the array and build a single string, then pass that string to MsgBox. There is no built-in way to hand an array straight to MsgBox; it only accepts a string (plus optional buttons and a title), so the loop is the actual answer. The shortest working pattern is: declare a string variable, loop from LBound to UBound, append each element with a line break, then call MsgBox on the result.
What You'll Need
- Microsoft Excel with the Developer tab enabled (File > Options > Customize Ribbon > check Developer). Labels can differ slightly by version and region.
- A workbook saved as
.xlsmif you want to keep the macro; standard.xlsxfiles discard VBA code on save. - Basic familiarity with the Visual Basic Editor (Alt+F11 opens it on Windows; on Mac use Tools > Macro > Visual Basic Editor).
Step-by-Step
- Open the Visual Basic Editor. Press Alt+F11 on Windows. On Mac, go to Tools > Macro > Visual Basic Editor. The editor opens in its own window.
- Insert a module. In the editor, choose Insert > Module. Code placed in a standard module can be run from Excel with Alt+F8. Code placed in a sheet module only runs in response to events on that sheet, which is rarely what you want for a quick test.
- Declare the array and fill it. For a fixed list, use a Variant array with
Array(). For example:Dim fruits As Variantfruits = Array("Apple", "Banana", "Cherry")
For a numeric array, declare it explicitly:Dim nums(1 To 3) As Longand assign each element. - Declare a string variable for the output. Add
Dim output As String. This holds the assembled text you will pass to the message box. - Loop through the array using LBound and UBound. Write:
Dim i As LongFor i = LBound(fruits) To UBound(fruits)output = output & fruits(i) & vbCrLfNext iLBoundandUBoundreturn the first and last valid indexes, so the loop works whether the array is 0-based or 1-based. Hard-coding0 To 2breaks the moment the array size changes. - Trim the trailing line break (optional but tidy). The loop above leaves a
vbCrLfat the end, which shows as a blank line in the message box. Addoutput = Left(output, Len(output) - Len(vbCrLf))after the loop, or build the string with a separator that you only add between items. - Call MsgBox with the string. Write
MsgBox output, vbInformation, "Array Contents". The first argument is the text, the second sets the icon (optional), and the third is the title bar text (optional). - Run the macro. Press F5 inside the editor, or return to Excel and use Alt+F8, select the macro name, and click Run. The message box appears with each array value on its own line.
Common Mistakes
- Passing the array directly.
MsgBox fruitsfails with a type mismatch becauseMsgBoxexpects a string. Always build the string first. - Hard-coding the loop bounds. Using
For i = 0 To 2on a 1-based array skips the first item and throws a subscript-out-of-range error on the last. UseLBoundandUBoundevery time. - Forgetting
vbCrLf. Without a line break, every value runs together on one line and becomes unreadable. - Declaring the array as a fixed-size type and then reassigning it with
Array(). A variable declaredDim x(1 To 3) As Longcannot be reassigned withArray(); use a Variant for that pattern. - Putting the code in the wrong module. Code in a sheet module will not appear in the Alt+F8 macro list unless it is a
Public Subwith no arguments. Use a standard module for testing. - Saving as
.xlsx. The macro disappears on close. Save as.xlsm.
Tips
- For a quick one-liner, use
Join:MsgBox Join(fruits, vbCrLf). It works on string arrays and Variant arrays of strings, and it handles the line breaks for you. It will not work on numeric arrays without converting each element first. - Use
vbInformation,vbExclamationorvbCriticalas the second argument to signal whether the output is informational or a warning. - If the array is large, a message box becomes unreadable. Write the values to a worksheet column or a text file instead, and keep
MsgBoxfor short arrays. - To show index and value together, build each line as
i & ": " & fruits(i). This makes it obvious which element is which when debugging. - For a two-dimensional array, nest a second loop over the second dimension and separate rows with an extra line break.
Troubleshooting
- "Compile error: Expected array" — the variable was declared as a plain String or Long but used with parentheses. Declare it as
Variantor with explicit dimensions. - "Subscript out of range" — the loop bounds do not match the array's real indexes. Replace hard-coded numbers with
LBoundandUBound. - "Type mismatch" — you passed the array itself to
MsgBox, or mixed numeric and string elements inJoin. Convert withCStrinside the loop. - Message box shows only the first value — the line-break concatenation is missing or the loop exits after one pass. Check that
Next icloses theFor. - Macro does not appear in Alt+F8 — the code is in a sheet module, is declared
Private, or takes arguments. Move it to a standard module and declare itPublic Sub. - Code vanished after saving — the file was saved as
.xlsx. Re-add the code and save as.xlsm.
Frequently Asked Questions
Can MsgBox display an array directly?
No. MsgBox accepts a string (plus optional button, title and help arguments). You must loop through the array and concatenate the elements into one string first, or use Join for string arrays.
How do I put each value on its own line?
Append vbCrLf after each element inside the loop, or use Join(arrayName, vbCrLf). Both produce one value per line in the message box.
Why does my loop skip the first element?
The array is probably 1-based while the loop starts at 0. Use For i = LBound(arrayName) To UBound(arrayName) so the bounds always match the array.
Can I show a numeric array in a message box?
Yes, but convert each element to text inside the loop with CStr or by concatenating it to a string variable. Join will not accept a numeric array directly.
How do I add a title or icon to the message box?
Use the second and third arguments: MsgBox output, vbInformation, "Results". The icon constant is optional; omit it and pass the title as the second argument if you prefer.
Does this work on Excel for Mac?
Yes. The VBA language and MsgBox behave the same. Only the route to the editor differs: Tools > Macro > Visual Basic Editor instead of Alt+F11.