VBA Ubound()不起作用?

时间:2013-03-24 12:10:18

标签: vba excel-2010

我正在尝试在VBA-Excel 2010中创建自己的函数来计算作为函数arg传递的数组中的一些内容。另外2个条件也通过了。问题是我无法使用Ubound()函数。

以下是代码:

Function IleGier(Arr As Variant, Champ As String, Data As Date) As Variant 'Integer
    '                arr/\           cond1/\        cond2/\
    Dim i As Integer
    Dim ile As Integer

    ile = -1
    i = 1

    Do While i < UBound(Arr, 1)
        If Arr(1)(i) = Data Then
            If Arr(2)(i) = Champ Then
                ile = ile + 1
            End If
        End If
        i = i + 1
    Loop

    IleGier = ile
    'IleGier = Arr(2)(1)
End Function

数组永远是二维的。

离。阵列:

15-3-2013 Arg1
15-3-2013 Arg2
15-3-2013 Arg1
15-3-2013 Arg1
16-3-2013 Arg3
16-3-2013 Arg3
16-3-2013 Arg1

=IleGier(E1:F10;"Arg1";"15-3-2013")的期望回报将是3(此例程中的日期arg可能会错误地通过“”),但只要我使用#ARG函数,它就会返回UBound()

任何提示?

2 个答案:

答案 0 :(得分:2)

i是第二个索引,我想你应该使用:

Do While i < UBound(Arr, 2)

答案 1 :(得分:0)

如果Excel中的数据如下所示:

enter image description here

...然后代码看起来像这样:

Public Function IleGier(Arr As Range, Champ As String, Data As Date) As Variant

    On Error GoTo IleGier_Error

    Dim arrValue As Variant
    Dim i As Integer
    Dim ile As Integer

    ile = 0
    i = 1
    arrValue = Arr.Value

    Do While i < UBound(arrValue, 1)
        If arrValue(i, 1) = Data Then
            If arrValue(i, 2) = Champ Then
                ile = ile + 1
            End If
        End If
        i = i + 1
    Loop

    IleGier = ile

    Exit Function

IleGier_Error:
    MsgBox Err.Description
End Function