最佳实践VBA:非布尔函数的布尔返回值

时间:2015-11-03 09:06:37

标签: arrays vba excel-vba excel

我正在寻找一些有关编写函数的帮助,以便在VBA中返回一行多维数组。 Cpearson has a very extensive article关于VBA中的数组函数,其中包含具有许多不错功能的“库”函数。在StackOverflow上的许多VBA数组问题中都引用了这篇文章。但是,我注意到cpearson因此使用了布尔函数。

示例:下面给出的GetRow函数是布尔函数,但我认为该函数应该返回给定行号的一维数组,如

Function RtrnArrayRow(SrcArr() As Variant, RowNumber As Integer) As Variant

其中RtrnArrayRow是一维数组。

问题:应用数组函数,布尔值或非布尔值以及如何最好地正确使用下面的布尔函数的最佳做法是什么?

所有帮助都是适用的!

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

2 个答案:

答案 0 :(得分:5)

填充/返回数组的函数通常是通过引用在参数中返回数组,因为这时函数的返回值可用于查看数组是否成功返回。

与VB.NET不同,在VB6 / A中,您无法通过Is Nothing轻松测试数组是否存在。因此,这种明确返回成功/失败的技术。

您可以使用它,或者您可以采用ways to test if an array exists之一,在这种情况下,您可以返回数组而不是布尔值。

答案 1 :(得分:0)

符合GSerg的解释:

该函数在处理&#34;输入&#34; 数组(Arr As Variant)之前进行一系列验证,以提取&#34;输出&#34; &#34;必需&#34; 行(ResultArr As Variant)的数组(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

否则会生成&#34;结果&#34;阵列

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
相关问题