Excel VBA Print Array: Step-by-Step Guide
In this article
To print an array in Excel VBA, you write the array's contents to a place you can see it: the Immediate Window with Debug.Print, a message box with MsgBox or Join, a worksheet range, or a file. Arrays themselves have no visible output. The simplest working line is Debug.Print Join(myArray, ", "), which prints a one-dimensional array in a single line in the Immediate Window. For anything more complex, you loop through the elements and write each one out. The steps below cover each option and the syntax that trips people up.
What You'll Need
- Microsoft Excel with the Developer tab enabled (File > Options > Customize Ribbon > tick Developer, or the Developer tab on the Ribbon in newer versions). Labels and locations can differ slightly by version and region.
- The Visual Basic Editor, opened with Alt+F11 on Windows or Option+F11 on Mac.
- A module: in the VBA Editor, use Insert > Module so your code has a place to live.
- Macros enabled in the workbook, or the file saved as .xlsm so your code is kept.
- For the Immediate Window, press Ctrl+G (Windows) or Control+G (Mac) in the VBA Editor, or use View > Immediate Window.
Step-by-Step
- Declare and fill your array. Start with a clear declaration. A fixed array looks like
Dim scores(1 To 5) As Long. A dynamic array looks likeDim names() As Stringand needsReDim names(1 To 3)before use. Then assign values, for examplescores(1) = 90and so on. Know whether your array is 0-based or 1-based, because that changes which elements you loop over. - Print with Debug.Print for quick inspection. In the Immediate Window, type or run code such as
Debug.Print scores(1). Inside a procedure, putDebug.Print scores(i)inside a loop. The output appears in the Immediate Window, not on a sheet, and it vanishes when you reset the project or close Excel. This is the fastest way to check what an array actually holds while you debug. - Print a one-dimensional array in one line with Join.
Joinconverts a one-dimensional array of strings (or values that convert cleanly to text) into one delimited string. UseDebug.Print Join(names, ", ")orMsgBox Join(names, vbCrLf)to show each item on its own line in a message box. Join fails on multi-dimensional arrays, and it errors if the array is not sized, so guard against that. - Loop for full control. When you need formatting, indexes, or multi-dimensional data, use a
Forloop. For a one-dimensional array:For i = LBound(arr) To UBound(arr)thenDebug.Print i, arr(i)thenNext i. UsingLBoundandUBoundmeans you never hard-code the bounds, so the code keeps working when the array size changes. - Handle two-dimensional arrays with nested loops. A 2D array such as
Dim grid(1 To 3, 1 To 4)needs an outer loop over the first dimension and an inner loop over the second:For r = LBound(grid, 1) To UBound(grid, 1), thenFor c = LBound(grid, 2) To UBound(grid, 2), thenDebug.Print grid(r, c). Build each row as a string with a separator, then print the row after the inner loop finishes. - Write the array to a worksheet to keep the output. This is the durable option. Declare a destination range whose size matches the array, then assign in one go: for a 1D array,
Range("A1").Resize(UBound(arr) - LBound(arr) + 1, 1).Value = Application.Transpose(arr). For a 2D array,Range("A1").Resize(UBound(arr, 1), UBound(arr, 2)).Value = arr. Transpose is needed for a 1D array because Excel expects a 2D block; it also has a limit on element count, so very large arrays should be looped or reshaped instead. - Write to a text file for a permanent log. Use
Open,Print #, andClose:Open "C:\temp\out.txt" For Output As #1, thenPrint #1, Join(arr, ","), thenClose #1. Check the path exists, and remember that Output overwrites the file while Append adds to it. - Run and check. Press F5 to run the procedure, or place the cursor inside it and press F8 to step through line by line. Watch the Immediate Window for output. If nothing appears, confirm you used the same module and that the Immediate Window is actually open.
Common Mistakes
- Declaring an array without sizing it. A dynamic array has no elements until you
ReDimit. Printing an unsized array gives a subscript out of range error. - Getting the boundaries wrong. Looping from 0 when the array starts at 1 skips or errors. Always use
LBoundandUBound. - Using Join on a 2D array. Join only accepts one dimension. Flatten the data into a string first, or loop.
- Forgetting Transpose for a 1D array. Assigning a 1D array directly to a range fills a row instead of a column, or errors depending on the target shape.
- Mixing up element counts and index values.
UBoundis an index, not a count. The count isUBound - LBound + 1. - Expecting Debug.Print to persist. The Immediate Window output is cleared on reset. Use a sheet or file if you need it later.
- Assigning to a Dimension mismatch. Writing
arr = Range("A1:C5").Valuecreates a 2D array even though it looks flat. Looping with one index will fail.
Tips
- Use
Debug.Printwith a tab separator (Debug.Print i, arr(i)) to align output into readable columns. - Print a header line first, such as
Debug.Print "Index", "Value", so you can tell which column is which. - For large arrays, add
DoEventsinside long loops or write to a file rather than the Immediate Window, which slows down with thousands of lines. - Test with a tiny array first — three or four elements — before running against real data.
- Name your arrays descriptively and add a comment saying whether they are 0-based or 1-based. It saves confusion later.
Troubleshooting
Nothing appears in the Immediate Window. Open it with Ctrl+G (Control+G on Mac), confirm the code actually reached the print line by stepping with F8, and make sure the procedure ran at all. Also check you are not looking at a different module's output.
Compile error or "Subscript out of range". The array was not sized, or you are addressing an index outside LBound to UBound. Print LBound(arr) and UBound(arr) first to see the real limits.
Type mismatch on Join. The array holds numbers or objects, not strings. Convert each element, or loop and print individually.
Range assignment fails. The target range size does not match the array's dimensions, or you omitted Transpose for a 1D array. Resize the destination and check both dimensions.
Message box is unreadable. Long arrays overflow a MsgBox. Use vbCrLf separators and keep it to short arrays, or switch to worksheet output.
Mac differences. Keyboard shortcuts vary slightly, and file paths in Open statements use Mac-style separators. Test any file-writing code on the target platform.
Frequently Asked Questions
How do I print an entire array at once?
For a one-dimensional string array, use Debug.Print Join(arr, ", "). Multi-dimensional arrays need a loop; there is no single built-in statement that prints all elements.
Why does Debug.Print output disappear?
It only lives in the Immediate Window of the current session. Pressing the reset button or closing Excel clears it. Write to a sheet or file to keep the output.
Can I print an array to a cell range?
Yes. Size the destination with Resize and assign the array directly for 2D arrays, or use Application.Transpose for 1D arrays. Very large arrays may need a loop instead.
What is the difference between LBound and UBound?
LBound returns the lowest valid index and UBound the highest. Together they let you loop safely without knowing whether the array starts at 0 or 1.
Can I print a 2D array with MsgBox?
Not directly. Build a string row by row with vbCrLf between rows and MsgBox the result, or print to a sheet for cleaner output.