arifulromadhon
arifulromadhon

Reputation: 71

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

I have been making validation in macro vba...

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

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.

Upvotes: 1

Views: 30051

Answers (2)

Dinesh Kumar
Dinesh Kumar

Reputation: 414

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

If (not cook = True) And (if (exp_gas > 0)  Then
     
MsgBox "He doesn't cook", vb Information 

End If

If((exp_woodburn > 0) or _(exp_oil > 0)) then
    
MsgBox "He doesn't cook", vbInformation 

End if

Upvotes: 0

Michał Turczyn
Michał Turczyn

Reputation: 37500

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

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

Upvotes: 2

Related Questions