vba Excel中的多个If条件
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/34500120/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Multiple If conditions in Excel
提问by Doug Coats
I clearly have a syntax issue and would like to know where am I magically missing the point. Got tired of trying to find the answer so I decided to ask.
我显然有语法问题,想知道我在哪里神奇地错过了这一点。厌倦了试图找到答案,所以我决定问。
Code just wants to check if Xis not any of three specific variables, and if not then P = 3, other Pwill equal 1or 2based on a combo box result.
代码只是想检查是否X不是三个特定变量中的任何一个,如果不是 then P = 3, otherP将等于1或2基于组合框结果。
I tried using Or statements with this and no luck.
我尝试使用 Or 语句,但没有运气。
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
回答by Denise Skidmore
If X <> 2 And X <> 3 And X <> 4 Then
P = 3
ElseIf ComboBox <> "OtherCondition" Then
P = 1
Else
P = 2
End If
回答by Matt Cremeens
I prefer
我更喜欢
If x <> 15 and x <> 18 and x <> 20 then
P = 3
ElseIf ComboBox <> "Other Condition" then
P = 1
Else
P = 2
End If
Your version has and Elsefollowed by an End If, which is incorrect syntax. This version is more succinct and easier to read, IMHO.
您的版本有和Else后跟End If,这是不正确的语法。恕我直言,这个版本更简洁易读。
回答by Scott Craner
Try this:
尝试这个:
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

