format for writing up an if statement with 2 conditions, one of which consists of 2 subconditions

yxz152830

Active Member
Joined
Oct 6, 2021
Messages
393
Office Version
  1. 365
Platform
  1. Windows
Gurus,
If I want to write an if statement which logic goes like this: if A or B then ...
B consists of say a=1 and b = 2.
So the statement looks like this:
If x=1 or a=2 and b =3 then
how do I write it in VBA. It seems like the above statement is ambiguous. It can be read as x or (a and b) or (x or a) and b.
 

Excel Facts

Can you AutoAverage in Excel?
There is a drop-down next to the AutoSum symbol. Open the drop-down to choose AVERAGE, COUNT, MAX, or MIN
Hello! Parentheses are used for spaces or arguments in group reasoning, especially for overriding the default operator precedence order in a complex expression. I. e.
VBA Code:
Sub ParenthTest()
Dim a%, b%, c%, d%
    a = 2
    b = 3
    c = 7
    d = b + c / a: MsgBox d ' result - 6
    d = (b + c) / a: MsgBox d ' result - 5
End Sub
So it's better to put parentheses once more than to omit them.
 
Upvote 0

Forum statistics

Threads
1,214,948
Messages
6,122,420
Members
449,083
Latest member
Ava19

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top