FWIW, the one thing the functions don't do is distinguish between
unallocated arrays and ranges or other non-array input.
I don't think it is a good idea to think of a Range object as an Array, even
though IsArray sometimes says it is so. I base this judgment on the
following problems that occur when pretending that an Range is an Array.
- You can't use LBound or UBound to get the bounds. This doesn't work:
Dim R As Range
Set R = Range("A1,C3")
Debug.Print LBound(R, 1)
- IsArray doesn't return a reliable True or False value. For example, R1 and
R2 below are perfectly valid Ranges but are not treated as arrays by
IsArray. Only for R3 will IsArray return True.
Dim R1 As Range
Dim R2 As Range
Dim R3 As Range
Set R1 = Range("A1")
Set R2 = Range("A1,C3")
Set R3 = Range("A1:C3")
Debug.Print "IsArray: R1: " & IsArray(R1) & " R2: " & IsArray(R2) & " R3:
" & IsArray(R3)
- Indexing does not work as one would expect if a Range were really an
array:
Dim R1 As Range
Set R1 = Range("A1,D4")
Debug.Print R1(2).Address
This displays $A$2, which is not part of the range R1. If a Range worked
like an Array, the code should display $D$4.
The code I posted works fine with real arrays, not these pseudo-array things
like a Range. If one is going to start extending the definition of an array,
would you consider the Worksheets object as an array? What about
Collections?
Conversely, I don't like the idea of using a For Next on an array. For
example, I do not like the following code:
Dim Arr(1 To 3)
Dim V As Variant
Arr(1) = 11
Arr(2) = 22
Arr(3) = 33
For Each V In Arr
Debug.Print V
Next V
--
Cordially,
Chip Pearson
Microsoft MVP - Excel, 10 Years
Pearson Software Consulting
www.cpearson.com
(email on the web site)