Subscript out of range error with an array – no idea why?

When you assign worksheet values to a variant array, you always end up with a 2-D array that is 1 based (e.g. 1 to something, 1 to something; never 0 to something, 0 to something). If you are getting values from a single column the second Rank is merely 1 to 1.

This can be proven with the following.

Dim x As Long, rArray As Variant, rng As Range

Set rng = Range("D4", Range("D4").End(xlDown))
rng.NumberFormat = "0" 'don't really understand why this is here
rArray = rng.Value

Debug.Print LBound(rArray, 1) & ":" & UBound(rArray, 1)
Debug.Print LBound(rArray, 2) & ":" & UBound(rArray, 2)

For x = UBound(rArray, 1) To LBound(rArray, 1) Step -1
    Debug.Print rArray(x, 1)
Next x

So you need to ask for the element in the first rank of the array; it is insufficient to just ask for the element.

Leave a Comment