我在尋找一些關於編寫函數來返回VBA中多維數組的行的幫助。 Cpearson has a very extensive article關於VBA中的數組函數,帶有一個具有許多很好功能的函數庫。本文在StackOverflow上的大量VBA數組問題中被引用。但是,我注意到cpearson因此使用了布爾函數。最佳做法VBA:非布爾函數上的布爾返回值
實施例:下面給出的GetRow
功能是布爾函數,雖然我認爲該函數應該從給定的行數返回一個一維陣列,像
Function RtrnArrayRow(SrcArr() As Variant, RowNumber As Integer) As Variant
其中RtrnArrayRow
是一維陣列。
問題:應用數組函數,布爾值還是非布爾值,以及如何正確使用下面的布爾函數的最佳做法是什麼?
所有幫助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
謝謝你的回答。確切地說*這個函數做的很清楚,我的問題是*爲什麼* :-) – Holene