How to print two dimensional array in Immediate window in VBA ? Does it exist any generic method for doing this ? Some method for ploting one row of array per line in Immedi
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
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 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.
No, you will either need to;
I wrote my own function for this. print_r() for VBA. It can evaluate Arrays, Dictionaries, Collection, MatchCollection etc.. The whole also nested. In addition, the data types are given and special characters are displayed in strings as Unicode.
http://wiki.yaslaw.info/doku.php/vba/functions/print_r/index
print_r(array(1,2,3,array("A","B")))
<Variant()> (
[0] => <Integer> 1
[1] => <Integer> 2
[2] => <Integer> 3
[3] => <Variant()> (
[0] => <String> 'A'
[1] => <String> 'B'
)
)
print_r([{1,2;3,4}])
<Variant()> (
[1.1] => <Double> 1
[1.2] => <Double> 2
[2.1] => <Double> 3
[2.2] => <Double> 4
)
'Define 2d array
arr = [ {"A",1; "B",2; "C",3 } ]: _
For r = LBound(arr, 1) To UBound(arr, 1): _
For c = LBound(arr, 2) To UBound(arr, 2): _
Debug.Print arr(r, c): _
Next c: _
Next
paste below data in column A(Name ) in column B (Age)
Name Age
A 10
B 20
C 30
D 40
E 50
F 60
G 70
and run the below code
Sub Array_Demo()
Dim arr() As String
Dim i As Integer, j As Integer
' fill the array with strings
Last = Range("A" & Rows.Count).End(xlUp).Row
ReDim arr(Last, 1)
For i = 0 To Last - 1
arr(i, 0) = Cells(i + 1, 1).Value
arr(i, 1) = Cells(i + 1, 2).Value
Next
' only one loop to display its contents !!!
Dim v As Variant
For Each v In arr
Debug.Print v
Next
End Sub
You can see the below output in Immediate window
Name
A
B
C
D
E
F
G
Age
10
20
30
40
50
60
70