整数的FILTER函数 - VBA

时间:2013-04-19 18:53:47

标签: vba

我搜索了网站,但没有成功,并尝试对此进行一些研究,但面临“类型不匹配”错误。

我将数组声明为整数类型,但FILTER函数似乎只适用于STRING。能告诉我如何使用FILTER函数进行整数吗?

If UBound(Filter(CntArr(), count)) > 0 Then msgbox "found"

End If

2 个答案:

答案 0 :(得分:1)

据我所知,您需要知道数组中是否存在指定的计数。您可以使用for loop

Dim found as Boolean
found = False
For i = 0 To UBound (CntArr())
   If CntArr(i) = count Then
      found = True
      Exit For
   End If
Next i
If found Then msgbox "found" End If

答案 1 :(得分:1)

下面,我创建了返回布尔值的IsIntegerInArray()函数。请遵循两个Sub,以获取整数数组声明的示例。将数组声明为Integer还可防止隐式数据转换引起的一些不必要的错误。

Sub test_int_array()
    Dim a() As Integer

    ReDim a(3)
    a(0) = 2
    a(1) = 15
    a(2) = 16
    a(3) = 8

    '''   expected result: 1 row for each integer in the array
    Call test_printing_array(a)
End Sub

Sub test_printing_array(arr() As Integer)
    Dim i As Integer

    For i = 1 To 20
        If IsIntegerInArray(i, arr) Then
            Debug.Print i & " is in array."
        End If
    Next i
End Sub

Function IsIntegerInArray(integerToBeFound As Integer, arr() As Integer) As Boolean
    Dim i As Integer

    '''   incorrect approach:
    '''   IsIntegerInArray = (UBound(Filter(arr, integerToBeFound)) > -1)   ' this approach searches for string, e.g. it matches "1" in "12"

    '''   correct approach:
    IsIntegerInArray = False
    For i = LBound(arr) To UBound(arr)
        If arr(i) = integerToBeFound Then
            IsIntegerInArray = True
            Exit Function
        End If
    Next i
End Function