OR & AND in Macro

Roderick_E

Well-known Member
Joined
Oct 13, 2007
Messages
2,051
Hi there,
I understand how to do embeded or/and functions in a formula but I guess I don't understand how to do it in a macro.

Example:
If Sheets("AIR SURCHARGE TEST1").Cells(i, 8) = Sheets("SURCHARGES (ACTIVE)").Cells(7, 19).Value OR AND Sheets("SURCHARGES (ACTIVE)").Cells(7, 2).Value ="all" AND Sheets("SURCHARGES (ACTIVE)").Cells(7, 3).Value = "all" AND Sheets("SURCHARGES (ACTIVE)").Cells(7, 4).Value = "all" AND Sheets("SURCHARGES (ACTIVE)").Cells(7, 5).Value = "all"

I'm going to want to build some more OR statements into this I have a 14 senarios. The above is just 2 possible (exact match OR if 4 columns on Surcharges (Active) all equal "all")

the compiler complains about syntax error, do I need ( ) somewhere???
Thanks for the help
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Maybe you want:

Rich (BB code):
If Sheets("AIR SURCHARGE TEST1").Cells(i, 8) = Sheets("SURCHARGES (ACTIVE)").Cells(7, 19).Value OR (Sheets("SURCHARGES (ACTIVE)").Cells(7, 2).Value ="all" AND Sheets("SURCHARGES (ACTIVE)").Cells(7, 3).Value = "all" AND Sheets("SURCHARGES (ACTIVE)").Cells(7, 4).Value = "all" AND Sheets("SURCHARGES (ACTIVE)").Cells(7, 5).Value = "all")

Hope that helps.
 
Upvote 0
this seems to work

Code:
If Sheets("AIR SURCHARGE TEST1").Cells(i, 8) = Sheets("SURCHARGES (ACTIVE)").Cells(7, 19).Value Or _
    (Sheets("SURCHARGES (ACTIVE)").Cells(7, 2).Value = "all") And Sheets("SURCHARGES (ACTIVE)").Cells(7, 3).Value = "all" _
    And Sheets("SURCHARGES (ACTIVE)").Cells(7, 4).Value = "all" _
    And Sheets("SURCHARGES (ACTIVE)").Cells(7, 5).Value = "all" Then
 
Upvote 0
this seems to work

Code:
If Sheets("AIR SURCHARGE TEST1").Cells(i, 8) = Sheets("SURCHARGES (ACTIVE)").Cells(7, 19).Value Or _
    (Sheets("SURCHARGES (ACTIVE)").Cells(7, 2).Value = "all") And Sheets("SURCHARGES (ACTIVE)").Cells(7, 3).Value = "all" _
    And Sheets("SURCHARGES (ACTIVE)").Cells(7, 4).Value = "all" _
    And Sheets("SURCHARGES (ACTIVE)").Cells(7, 5).Value = "all" Then

Ok let me test it...
 
Upvote 0
It depends on some of your options and if you declare option excplicit at the top of the code. TexasALynn may be missing a parentheses, did you try mine? Also you don't need to test for all and ALL you can do:

Code:
ucase(("SURCHARGES (ACTIVE)").Cells(7, 5).Value) = "ALL"
or:

Code:
lcase(("SURCHARGES (ACTIVE)").Cells(7, 5).Value) = "all"
Hope that helps.
 
Upvote 0

Forum statistics

Threads
1,214,591
Messages
6,120,427
Members
448,961
Latest member
nzskater

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