2015-11-03 87 views
2

我在尋找一些關於編寫函數來返回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 

回答

5

功能用於填充/回報陣列通常由通過參考中的一個參數返回該陣列,因爲這樣的函數的返回值可以用於以查看是否已成功返回的數組。

與VB.NET不同,在VB6/A中,通過執行Is Nothing不能輕鬆測試數組是否存在。因此這種顯式返回成功/失敗的技巧。

您可以使用它,或者您可以採用ways to test if an array exists之一,在這種情況下,您可以返回數組而不是布爾值。

0

與GSerg的解釋行:

的函數執行處理「輸入」陣列(Arr As Variant)提取「輸出」陣列(ResultArr As Variant)爲之前的一系列的驗證的「required」 row(RowNumber As Long)如果所有的驗證都成功通過,那麼它返回預期的結果作爲參數,其值變爲TRUE;如果任何驗證失敗,其值爲FALSE,那麼您知道輸出無效。

驗證:

' Ensures Arr is an array. 
' Ensures Arr is a two-dimensional array. 
' Ensures ResultArr is a dynamic array. 
' Ensures ColumnNumber is less than or equal to the number of columns. 

如果上述任何失敗,則

GetRow = False 
    Exit Function 

否則產生 「結果」 陣列

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 

並變爲TRUE

GetRow = True 

所有內容都在函數的開頭進行了說明。

'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 
' 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. 
'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' 

此功能旨在以這種方式行事,以傳遞信息,它是否能夠準確地執行什麼它應該做的。這是它可以使用的形式之一:

If not GetRow(InputArray, ResultArray , RowNumber) then Goto ErrorHandler 
+1

謝謝你的回答。確切地說*這個函數做的很清楚,我的問題是*爲什麼* :-) – Holene