I'm doing VBA macros and need to use Arrays of values extensively. After the below help from @TonyDallimore on Stack Overflow, I've decided to use nested variant arrays.
VBA chrashes when trying to execute generated sub
I use multidimensional, jagged arrays to SELECT
data from DB's and efficiently perform calculations and write to a Worksheet. The data is sometimes a single value (Rank:0) sometimes a row of values (Rank:1) sometimes a table of values with some cells containing rows of values (Rank:3). I use the function below to determine what kind of operations are possible and should be performed to such arrays.
This function, along with all my array related functions reside in a module: modCustomArrayFunctions.
'***************************************************************************'
'Returns the rank of the passed array '
'Parameters: '
' Arr: The array to be processed '
'Returns: '
' The rank of the array '
'***************************************************************************'
Public Function Rank(ByRef Arr As Variant) As Byte
'Declarations **************************************************************'
Dim MaxRank As Byte 'Maximum rank of the elements of the array '
Dim i As Integer
'***************************************************************************'
If IsArray(Arr) Then
If IsArrInitialized(Arr) Then
MaxRank = 0
For i = LBound(Arr) To UBound(Arr)
Rank = Rank(Arr(i)) + 1
If Rank > MaxRank Then MaxRank = Rank
Next i
Rank = MaxRank
Else
Rank = 0
End If
Else
Rank = 0
End If
End Function
'***************************************************************************'
I code my routines pretty much this way.
And the accompanying test is as follows:
'***************************************************************************'
Public Sub Rank_Test()
Dim TestArr As Variant
Dim TestRank As Integer
'Test Non-Array
If Rank(TestArr) = 0 Then
Debug.Print "Non-Array OK"
Else
Debug.Print "Non-Array FAILED!"
End If
'Test Ranks 1 to 100
For TestRank = 1 To 100
TestArr = MakeArray(TestRank)
If Rank(TestArr) = TestRank Then
Debug.Print TestRank & "D OK"
Else
Debug.Print TestRank & "D FAILED!"
End If
Next TestRank
End Sub
'***************************************************************************'
Can the code, comments and test be deemed acceptable, what is there to improve? Is the test okay or have I got the whole unit testing idea wrong?
IsArrInitialized()
and MakeArray(n)
are listed for completeness here. Note that MakeArray(n)
is only used to create test arrays and is private to the array test module.
'***************************************************************************'
Private Function MakeArray( _
RankArr As Integer, _
Optional Value As Variant = 1 _
) As Variant
Dim TestArr As Variant
Dim DummyArr As Variant
Dim i As Integer
If RankArr = 0 Then
MakeArray = Value
ElseIf RankArr = 1 Then
ReDim TestArr(1 To 1)
TestArr(1) = Value
MakeArray = TestArr
Else
ReDim TestArr(1 To 1)
ReDim DummyArr(1 To 1)
DummyArr(1) = Value
For i = 1 To RankArr - 1
DoEvents
TestArr(1) = DummyArr
DummyArr = TestArr
Next i
MakeArray = TestArr
End If
End Function
'***************************************************************************'
'***************************************************************************'
'Determines if a dynamic array has been initialized '
'Parameters: '
' Arr: The array to be processed '
'Returns: '
' True if initialized, False if not '
'***************************************************************************'
Public Function IsArrInitialized(ByRef Arr As Variant) As Boolean
'Declarations **************************************************************'
Dim dum As Long
'***************************************************************************'
On Error GoTo ErrLine
If IsArray(Arr) Then
dum = LBound(Arr)
If dum > UBound(Arr) Then
GoTo ErrLine
Else
IsArrInitialized = True
End If
Else
IsArrInitialized = False
End If
Exit Function
ErrLine:
IsArrInitialized = False
End Function
'***************************************************************************'