[英]Visual Basic - find occurrence of a value in an array
就像标题所说,我需要找出我的值在数组中出现多少次。 在这种情况下,我要查找整数0出现多少次,并返回整数0s。 有人知道语法是什么吗?
只需将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
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.