如果条件为"和"结合"多个或"在Vba Excel中

时间:2017-08-16 04:10:44

标签: excel vba excel-vba if-statement

我一直在使用macro vba进行验证...

如果使用"和"结合" multiple或"在vba?

这是我的代码:

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

它给我语法错误。

2 个答案:

答案 0 :(得分:1)

您无法在另一个If语句中使用If语句。一种解决方案是使用嵌套的If' s,以获取更多信息here。另一个解决方案是摆脱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

答案 1 :(得分:0)

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

if(not cook = True)和(if(exp_gas> 0)然后

MsgBox"他不做饭#34;,vb信息

结束如果

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

MsgBox"他不会做饭#34;,vbInformation

如果

结束