簡體   English   中英

Excel VBA IN關鍵字

[英]Excel VBA IN keyword

有沒有更好的方法在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。 語法中是否有替代關鍵字? 謝謝!

FILTER()函數的目的是查看項目是否出現在數組中:

Sub FilterExample()
    ary = Split("a,b,c,d", ",")
    x = "b"
    If Filter(ary, x)(o) <> "" Then
        MsgBox "match found"
    End If
End Sub

當沒有找到匹配時,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

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM