I am running into some issues trying to populate a multidimensional variant array. What I am trying to do is add a single value to row 1, column 1 in the array. However the following code does not work.
Dim arr() As Variant
arr(1,1) = 3
Building on this, I am wishing to add more values to the array when certain conditions are met. eg
For c = 2 To 10001
Sheets("Data").Select
If Range("P" & c) = branch And Range("Q" & c) = dept And Range("R" & c) = subdept Then
arr(UBound(arr) + 1, 1) = Range("A" & c)
End If
Next c
After the loop has finished and I have finished extracting the values I am after I wish to redim it as a variable length array. However,
Redim arr()
also is not working. Also, I should point out that I have no real need for the array to be multidimensional, just variable in size. Saying that, I do not know of a way to create a 1 dimensional dynamic array in VBA.
To summarize
1) Why does arr(1,1) throw me an error?
2) Why does Redim arr() throw me an error? (I do not want to redim to a fixed size)
3) Is there a way to create a dynamic 1D array in VBA?
Note: I suppose I could declare a 1D 100 length array which I could then manipulate to work out the number of filled values in it and redim it as (0 to 100), but first wish to explore if there are any answers to my above questions.
Many thanks.
UPDATE: (my second attempt: still fails on arr(arrLen, 1) = Range("A" & c))
arrLen = 1
For c = 2 To 10001
Sheets("Data").Select
If Range("P" & c) = branch And Range("Q" & c) = dept And Range("R" & c) = subdept Then
ReDim Preserve arr(arrLen To 1)
arr(arrLen, 1) = Range("A" & c)
arrLen = arrLen + 1
End If
Next c