My example is for a 2D array, and it could be addapted for a 1D to n Array but I guess I will now be more specific, showing how to use a 1D and a 2D, and for more dimesions you will need to increase the amount of loops proportionally in the 2D example. Copy the below 3 Subs and just call the TestArray
Public Sub TestArray()
Dim my1DArray(3) As String
Dim my2DArray(3, 4) As String
my1DArray(0) = "First item"
my1DArray(3) = "Fourth item"
Debug_1DArray my1DArray
my2DArray(1, 1) = "Test1"
my2DArray(2, 2) = "Test2"
my2DArray(3, 3) = "Test3"
Debug_2DArray my2DArray
End Sub
Public Sub Debug_1DArray(ByVal myArray As Variant)
Dim vItem As Variant
Dim iCnt As Long
On Error GoTo ErrDone
iCnt = LBound(myArray)
For Each vItem In myArray
Debug.Print "Item " & iCnt; " Value: " & vItem
iCnt = iCnt + 1
Next vItem
ExitProceedure:
Exit Sub
ErrDone:
GoTo ExitProceedure
End Sub
Public Sub Debug_2DArray(ByVal myArray As Variant)
Dim x As Long
Dim y As Long
On Error GoTo ErrDone
'Yes, I know, using LBound is better than using 0
For x = LBound(myArray, 1) To UBound(myArray, 1)
For y = LBound(myArray, 2) To UBound(myArray, 2)
Debug.Print "x: " & x; " y: " & y; " Value: " & myArray(x, y)
Next y
Next x
ExitProceedure:
Exit Sub
ErrDone:
GoTo ExitProceedure
End Sub