在范围内的VBA数组中查找至less一个项目
我目前正在试图从一个范围内的数组中find至less一个项目,但是我目前正在执行的方法会检查所有值是否在范围内。
If Not accountRange.find(Array(11571, 11572, 11573, 11574, 11575)) Is Nothing Then
理想情况下,我会有这样的事情:
If Not accountRange.find(Array(11571, xlOr, 11572, xlOr, 11573, xlOr, 11574, xlOr, 11575))
我能看到的唯一解决方法是单独检查每个条件,如下所示:
If Not accountRange.find(11571) Is Nothing Or _ Not accountRange.Find(11572) Is Nothing Or _ Not accountRange.Find(11573) Is Nothing Or _ Not accountRange.Find(11574) Is Nothing Or _ Not accountRange.Find(11575) Is Nothing Then
但是当我有更多的事情要检查范围时,显然这将是乏味的。
有什么build议么?
你可以写一个这样的函数:
Function ValInRange(vals As Variant, R As Range) As Boolean Dim item As Variant For Each item In vals If Not R.Find(item) Is Nothing Then ValInRange = True Exit Function End If Next item End Function
然后, ValInRange(Array(11571, 11572, 11573, 11574, 11575), accountRange)
将返回True
如果其中一个传入的数字在范围内。 否则,它将返回False
的默认布尔值。