I'm using a 2D dynamic array, for data in rows and columns.
The number of rows increases/decreases.
The array data doesn't seem to be stored. For code line "MsgBox my_arr(2, 2)", a blank appears.
Not sure if this is the reason:
With multidimensional dynamic arrays, only the last dimension can be increased, unless you use a workaround, such as from here:
Dynamic Array with ReDim Preserve VBA - wellsr.com
This is included below.
The number of rows increases/decreases.
The array data doesn't seem to be stored. For code line "MsgBox my_arr(2, 2)", a blank appears.
Not sure if this is the reason:
With multidimensional dynamic arrays, only the last dimension can be increased, unless you use a workaround, such as from here:
Dynamic Array with ReDim Preserve VBA - wellsr.com
This is included below.
Heading 1 | Heading 2 | Heading 3 | Heading 4 |
Area 1 | Apples | Pears | Oranges |
Area 1 | Watermelon | Nectarines | Lemons |
Area 3 | Rockmelon | Oranges | Nectarines |
Area 4 | Pears | Watermelon | Rockmelon |
Area 5 | Lemons | Oranges | Rockmelon |
Area 5 | Nectarines | Lemons | Watermelon |
VBA Code:
Option Explicit
Dim my_arr() As Variant
Sub two_D_array()
Dim my_row As Long, last_row As Long, last_col As Long
Dim i As Long
Application.ScreenUpdating = False
Debug.Print String(65535, vbCr)
last_row = ActiveSheet.Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
last_col = ActiveSheet.Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column
ReDim my_arr(0, 0): ReDim my_arr(0, 1): ReDim my_arr(0, 2): ReDim my_arr(0, 3)
'Data
'----
i = 0
For my_row = 1 To last_row
'populate arrays
my_arr(i, 0) = Cells(i + 1, 1).Value: Debug.Print my_arr(i, 0)
my_arr(i, 1) = Cells(i + 1, 2).Value: Debug.Print my_arr(i, 1)
my_arr(i, 2) = Cells(i + 1, 3).Value: Debug.Print my_arr(i, 2)
my_arr(i, 3) = Cells(i + 1, 4).Value: Debug.Print my_arr(i, 3)
'Resize arrays ... roundabout way required ... see function
ReDim my_arr(i, 0): ReDim my_arr(i, 1): ReDim my_arr(i, 2): ReDim my_arr(i, 3)
my_arr = redim_pres(my_arr, i + 1, 0): my_arr = redim_pres(my_arr, i + 1, 1): my_arr = redim_pres(my_arr, i + 1, 2): my_arr = redim_pres(my_arr, i + 1, 3)
i = i + 1
Debug.Print "-----------"
Next my_row
Application.ScreenUpdating = True
MsgBox my_arr(2, 2)
End Sub
Private Function redim_pres(MyArray As Variant, nNewFirstUBound As Long, nNewLastUBound As Long) As Variant
Dim i, j As Long
Dim nOldFirstUBound, nOldLastUBound, nOldFirstLBound, nOldLastLBound As Long
Dim TempArray() As Variant
redim_pres = False
If Not IsArray(MyArray) Then MsgBox "You didn't pass the function an array.", vbCritical, "No Array Detected": End
nOldFirstUBound = UBound(MyArray, 1): nOldLastUBound = UBound(MyArray, 2)
nOldFirstLBound = LBound(MyArray, 1): nOldLastLBound = LBound(MyArray, 2)
ReDim TempArray(nOldFirstLBound To nNewFirstUBound, nOldLastLBound To nNewLastUBound)
For i = LBound(MyArray, 1) To nNewFirstUBound
For j = LBound(MyArray, 2) To nNewLastUBound
If nOldFirstUBound >= i And nOldLastUBound >= j Then
TempArray(i, j) = MyArray(i, j)
End If
Next
Next
If IsArray(TempArray) Then redim_pres = TempArray
End