Is there a better way to code this in VBA:
If x = 'a' or x = 'b' or x ='c' or x = 'd' then
...action
End if
The IN keyword would come very handy here:
If x IN ('a','b','c','d') then
action
End If
--> but VBA doesn't recognise IN. Is there an alternative keyword to it in the syntax? Thank you!
The purpose of the FILTER() function is to see if an item appears in an array:
Sub FilterExample()
ary = Split("a,b,c,d", ",")
x = "b"
If Filter(ary, x)(o) <> "" Then
MsgBox "match found"
End If
End Sub
Gary's Student's answer in its current form will cause an error when a match is not found. I believe the following is better:
x = "a"
If UBound(Filter(Array("a", "b", "c", "d"), x)) > -1 Then
MsgBox "Match found"
End If
I do comment though that when x is an empty string the filter function returns the whole array. So if x is an empty string the above will always return a match. The below accounts for this
x = "a"
If UBound(Filter(Array("a", "b", "c", "d"), x)) > -1 And x <> vbNullString Then
MsgBox "Match found"
End If
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.