VBA Multiple condition formatting for OR statements.

figuare9

Board Regular
Joined
Oct 23, 2017
Messages
118
So I'm looking to do something in VBA that has multiple conditions..

HTML:
Dim Cutwidth1butt2 As Integer
Cutwidth1butt2 = [M7].Value - [E14].Value

Code:
If [C14] = "2" Or [C15] = "3" Or [C16] = "4" Or [C17] = "5" And [D14] <> "" Or [D15] <> "" Or [D16] <> "" Or [D17] <> "" And [E14] <> "" Or [E15] <> "" Or [E16] <> "" Or [E17] <> "" And [K14] = Cutwidth1butt2 And [J14] <= [D13] Then


First set of conditions..

So essentially, If C14:C17 has either 2,3,4,5

AND D14:D17 Essentially has data...

AND E14:E17 Also has data...


Second Set of Conditions

K14 must equal Cutwidth1butt2

AND J14 <= D13


My issue is that this code is running right through.. It's always passing. Even when K14 doesn't equal the cutwidth.. As far as what I've experimented with previously, multiple "And" conditions work. But when I tried to mix "Or" conditions, it seems to behave differently? I'm a beginner in VBA so be easy on me! haha.


Full Code

Code:
        '[IF THERE IS ANY TAIL ATTACHED TO THE SECTION] AND CUT WIDTH IS THE SAME WIDTH AS THE "CUTWIDTH"
            If [C14] = "2" Or [C15] = "3" Or [C16] = "4" Or [C17] = "5" And [D14] <> "" Or [D15] <> "" Or [D16] <> "" Or [D17] <> "" And [E14] <> "" Or [E15] <> "" Or [E16] <> "" Or [E17] <> "" And [K14] = Cutwidth1butt2 And [J14] <= [D13] Then
            [D13].Value = [D13] - [J14]
            [D14].Value = [D14] + [J14]
                'Disable Button
                    MakeCut2.Enabled = False
                'Has cut been made?
                    [M14].Value = "Yes"
                    MsgBox Test
                    
            End If
 
Last edited:

Excel Facts

What is =ROMAN(40) in Excel?
The Roman numeral for 40 is XL. Bill "MrExcel" Jelen's 40th book was called MrExcel XL.
When mixing AND and OR statements, it is important to use parentheses to block off each condition.
Otherwise, it cannot tell how you want them blocked and it probably won't work the way you want.

If I understand you correctly, it should look something like this:
Code:
If ([C14] = "2" Or [C15] = "3" Or [C16] = "4" Or [C17] = "5") And ([D14] <> "" Or [D15] <> "" Or [D16] <> "" Or [D17] <> "") And ([E14] <> "" Or [E15] <> "" Or [E16] <> "" Or [E17] <> "") And ([K14] = Cutwidth1butt2) And ([J14] <= [D13]) Then
 
Last edited:
Upvote 0
That was all I needed.. Thank you very much for that! ha! Can't believe how simple that was. So if I use multiple "Ands" I don't need it.. Only when I mix "Ors" ? You're awesome!!!!
 
Upvote 0
So if I use multiple "Ands" I don't need it.. Only when I mix "Ors" ? You're awesome!!!!
Yes, if they were all ANDs, then order doesn't matter, because no matter how you group it, all would need to be TRUE to return TRUE.
Likewise, if they are all ORs, then order wouldn't matter either because no matter how you group those, as long as one OR is TRUE, the whole thing will return TRUE.
But when you start mixing AND and ORs, you will need to do that to get it to process in the order than you desire.
I believe with out parentheses, it just processed left to right, one at a time.
 
Upvote 0
Makes sense. Thank you very much for the explanation! Appreciate the help more then you know!! *Free hug* :) ha.

Take care.
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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