I was looking for some help regarding writing a function for returning a row of a multidimensional array in VBA. Cpearson has a very extensive article on array functions in VBA, with a "library" of functions with a lot of nice features. This article is cited in lots of VBA array questions on StackOverflow. However, I notice that cpearson consequently make use of boolean functions.
Example: the GetRow
function given below is boolean function, though I think that the function should return an one-dimensional array from the given row number, something like
Function RtrnArrayRow(SrcArr() As Variant, RowNumber As Integer) As Variant
where RtrnArrayRow
is a one-dimensional array.
Question: What is best-practice for applying array functions, boolean or non-boolean, and how to best make proper use of the boolean function below?
All help is appreactiated!
Function GetRow(Arr As Variant, ResultArr As Variant, RowNumber As Long) As Boolean
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
' GetRow
' This populates ResultArr with a one-dimensional array that is the
' specified row of Arr. The existing contents of ResultArr are
' destroyed. ResultArr must be a dynamic array.
' Returns True or False indicating success.
''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
Dim ColNdx As Long
''''''''''''''''''''''''''''''
' Ensure Arr is an array.
''''''''''''''''''''''''''''''
If IsArray(Arr) = False Then
GetRow = False
Exit Function
End If
''''''''''''''''''''''''''''''''''
' Ensure Arr is a two-dimensional
' array.
''''''''''''''''''''''''''''''''''
If NumberOfArrayDimensions(Arr) <> 2 Then
GetRow = False
Exit Function
End If
''''''''''''''''''''''''''''''''''
' Ensure ResultArr is a dynamic
' array.
''''''''''''''''''''''''''''''''''
If IsArrayDynamic(ResultArr) = False Then
GetRow = False
Exit Function
End If
''''''''''''''''''''''''''''''''''''
' Ensure ColumnNumber is less than
' or equal to the number of columns.
''''''''''''''''''''''''''''''''''''
If UBound(Arr, 1) < RowNumber Then
GetRow = False
Exit Function
End If
If LBound(Arr, 1) > RowNumber Then
GetRow = False
Exit Function
End If
Erase ResultArr
ReDim ResultArr(LBound(Arr, 2) To UBound(Arr, 2))
For ColNdx = LBound(ResultArr) To UBound(ResultArr)
ResultArr(ColNdx) = Arr(RowNumber, ColNdx)
Next ColNdx
GetRow = True
End Function