Excel中的多个条件

我清楚地有一个语法问题,并想知道我在哪里奇迹般地错过了这一点。 厌倦了试图find答案,所以我决定问。

代码只是想检查X是不是三个特定variables中的任何一个,如果不是,那么P = 3 ,其他P将基于combobox结果等于12

我尝试使用或声明与此并没有运气。

  If X <> 15 Then P = "3" Else If X <> 18 Then P = "3" End If Else If X <> 20 Then P = "3" End If ElseIf ComboBox <> "Other Condition" Then P = 1 Else: P = 2 End If 

 If X <> 2 And X <> 3 And X <> 4 Then P = 3 ElseIf ComboBox <> "OtherCondition" Then P = 1 Else P = 2 End If 

尝试这个:

 If x = 15 Or x = 18 Or x = 20 Then If ComboBox = "Other Condition" Then P = 2 Else P = 1 End If Else P = 3 End If 

我更喜欢

 If x <> 15 and x <> 18 and x <> 20 then P = 3 ElseIf ComboBox <> "Other Condition" then P = 1 Else P = 2 End If 

你的版本有和Else然后是一个End If ,这是不正确的语法。 这个版本更简洁,更容易阅读,恕我直言。