如果条件与“和”结合在 Vba Excel 中的“多个或”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/45704996/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-12 13:05:52  来源:igfitidea点击:

if condition with "and" combine "multiple or" in Vba Excel

excelvbaexcel-vbaif-statement

提问by arifulromadhon

I have been making validation in macro vba...

我一直在用宏 vba 进行验证...

How to make if condition with "and" combined with "multiple or" in vba?

如何在vba中使用“and”与“multiple or”组合条件?

Here is my code:

这是我的代码:

If (not cook = True) And (if (exp_gas > 0) or (exp_woodburn > 0) or _
    (exp_oil > 0) or (margarine > 0)) Then
    MsgBox "He doesn't cook", vbInformation 

End If

It gives me syntax error.

它给了我语法错误。

回答by Micha? Turczyn

You can't use Ifstatement in condition in another Ifstatement. One solution is to use nested If's, for more info look here. Another solution is to get rid of Ifinside condition, so your code would look like this (i think this is exactly what you need):

您不能If在另一个If语句中使用条件中的语句。一种解决方案是使用嵌套If的,有关更多信息,请查看此处。另一个解决方案是摆脱If内部条件,因此您的代码将如下所示(我认为这正是您所需要的):

If (not cook = True) And ((exp_gas > 0) or (exp_woodburn > 0) or _
    (exp_oil > 0) or (margarine > 0)) Then
    MsgBox "He doesn't cook", vbInformation 

End If

回答by Dinesh Kumar

the compiler cannot understand these conditions. we need to split the conditions and display the required. please use multiple if statements for this

编译器无法理解这些条件。我们需要拆分条件并显示所需的。请为此使用多个 if 语句

If (not cook = True) And (if (exp_gas > 0) Then

If (not cook = True) And (if (exp_gas > 0) Then

MsgBox "He doesn't cook", vb Information

MsgBox "他不做饭", vb 信息

End If

万一

If((exp_woodburn > 0) or _(exp_oil > 0)) then

如果((exp_woodburn > 0) 或 _(exp_oil > 0)) 那么

MsgBox "He doesn't cook", vbInformation

MsgBox "他不做饭", vbInformation

End if

万一