How to print two dimensional array in Immediate window in VBA?
If this is for debugging purposes, it's not convenient to write a macro to view the contents of the array during program execution. Might even cause problems.
For debugging during program execution, you'll want a code-free method you can use across all VB projects, to spy the contents of your array.
- In VBA IDE, click View menu > Locals Window
- In Local pane, find the array-name.
- Expand the nodes to find your values. The nodes will differ, depending on the type of array.
In this example, "aLookList" is a variant array. The values under "Value2" come from a range.
Another answer here suggests using the Watch pane. This is similar to my answer, but poster did not explain that you can spy the entire array (all cells) by simply adding the array name to Watch. Then , drill down nodes. The advantage of the Locals Window over the Watch Window, is that with Locals pane, you do not have to manually add the array to the pane, it's already there. So it's a bit less effort.
I made a simple loop to do this for anybody's reference:
Sub WriteArrayToImmediateWindow(arrSubA As Variant)
Dim rowString As String
Dim iSubA As Long
Dim jSubA As Long
rowString = ""
Debug.Print
Debug.Print
Debug.Print "The array is: "
For iSubA = 1 To UBound(arrSubA, 1)
rowString = arrSubA(iSubA, 1)
For jSubA = 2 To UBound(arrSubA, 2)
rowString = rowString & "," & arrSubA(iSubA, jSubA)
Next jSubA
Debug.Print rowString
Next iSubA
End Sub
No, you will either need to;
- Create & call a function that loops & prints it out to the debug window.
- If this is for debugging, right click the variable in the IDE & "Add Watch" which will bring up a window that will track changes to the value of the array & display its content when a breakpoint is hit.