Visual Basic - 在数组中查找值的出现

时间:2017-07-20 07:58:58

标签: arrays vba

正如标题所说,我需要找到我的数值在我的数组中出现的次数。在这种情况下,我正在寻找整数0出现的次数,并且它返回0的总量。任何人都知道它的语法是什么?

1 个答案:

答案 0 :(得分:0)

只需将SomeArray替换为您已为其他代码指定的Array

Sub CountIntInArray()

    Dim SomeArray As Variant

    Dim ArrayPos As Long
    Dim Num0s As Long
    Num0s = 0

    'Assuming you only have one level in the array
    'otherwsie you will need to check each level as well
    'See the help file on Ubound for more info
    For ArrayPos = 0 To UBound(SomeArray)

        If SomeArray(ArrayPos) = 0 Then
            Num0s t = Num0s + 1
        End If

        'If you want to ignore the decimals then you can use
        If Int(SomeArray(ArrayPos)) = 0 Then
            Num0s t = Num0s + 1
        End If

    Next ArrayPos

End Sub