VBA如果<any of =“”这些=“”> = <值>?</value> </any>

时间:2014-07-17 12:27:27

标签: vba if-statement logic

我对VBA相当新,而且我找不到一种简单的方法来测试任何指定的变量是否等于指定的值。以下似乎有效,但有更简单的方法吗?

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

我也试过以下,没有运气。

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

1 个答案:

答案 0 :(得分:14)

您可以使用选择案例:)

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