有没有更好的方法在VBA中对此进行编码:
If x = 'a' or x = 'b' or x ='c' or x = 'd' then
...action
End if
IN关键字在这里非常方便:
If x IN ('a','b','c','d') then
action
End If
- >但是VBA不承认IN。语法中是否有替代关键字? 谢谢!
答案 0 :(得分:1)
FILTER()功能的目的是查看某个项目是否出现在数组中:
Sub FilterExample()
ary = Split("a,b,c,d", ",")
x = "b"
If Filter(ary, x)(o) <> "" Then
MsgBox "match found"
End If
End Sub
答案 1 :(得分:1)
Gary的学生在当前表格中的答案将在未找到匹配时导致错误。我相信以下更好:
x = "a"
If UBound(Filter(Array("a", "b", "c", "d"), x)) > -1 Then
MsgBox "Match found"
End If
我做的评论虽然当x是一个空字符串时,filter函数返回整个数组。因此,如果x是一个空字符串,则上面将始终返回匹配项。以下说明了这个
x = "a"
If UBound(Filter(Array("a", "b", "c", "d"), x)) > -1 And x <> vbNullString Then
MsgBox "Match found"
End If