1

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

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

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

End If

4

2 回答 2

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
于 2013-04-19T19:15:55.483 回答
1

下面我创建了返回布尔值的 IsIntegerInArray() 函数。跟随两个 Subs 以获取整数数组声明的示例。将数组声明为 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
于 2019-03-07T08:45:38.107 回答