VBA If <any of these> = <value>?

扶醉桌前 提交于 2019-12-10 02:24:56

问题


I'm fairly new to VBA, and I can't find an easy way to test if any of the specified variables equal a specified value. The below seems to work, but is there an easier way to do it?

If variable1 = 1 Or variable2 = 1 Or variable3 = 1 Or variable4 = 1 Or variable5 = 1 Then End If

I've also tried the following, with no luck.

If (variable1 Or variable2 Or variable3 Or variable4 Or variable5) = 1 Then End If

回答1:


You can use select case :)

Sub Sample()
    Dim variable1, variable2, variable3, variable4, variable5

    variable1 = 1: variable2 = 1: variable3 = 1: variable4 = 1: variable5 = 1

    Select Case 1
        Case variable1, variable2, variable3, variable4, variable5
            MsgBox "One of them is equal to 1"
        Case Else
            MsgBox "none of then is equal to 1"
    End Select
End Sub


来源:https://stackoverflow.com/questions/24803580/vba-if-any-of-these-value

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!