formula If, And, Or

Lady Zero

New Member
Joined
Mar 3, 2025
Messages
2
Office Version
  1. 365
Platform
  1. Windows
Hello,
my formula needs to check if (B2=1 and A2 starts with '280') if this is so, write ok or else check if (B2=2 and A2 starts with '474') if this is so, write OK, or else check if (B2=3 and A2 starts with '860') if this is so, write ok; or else check if (B2=4 and A5 starts with '358') if this is so, write ok, or else check if (B2=0 and A2 starts with '358') if this is so, write ok; if none of this is so, write NOK.
So there are 5 'if and' formules and 'or else' parts added.

thanks for the help.
I cannot add my excel because it contains confidential information.

KR
LZ
 
Welcome to the Board!

You can just use OR and ANDs, i.e.
Excel Formula:
=IF(OR(AND(A2=1,LEFT(B2,3)="280"),AND(A2=2,LEFT(B2,3)="474"),AND(A2=3,LEFT(B2,3)="860"),AND(A2=4,LEFT(B2,3)="358"),AND(A2=0,LEFT(B2,3)="358")),"OK","")
or more simply:
Excel Formula:
=IF(OR(AND(A2=1,LEFT(B2,3)="280"),AND(A2=2,LEFT(B2,3)="474"),AND(A2=3,LEFT(B2,3)="860"),AND(OR(A2=4,A2=0),LEFT(B2,3)="358")),"OK","")

Note that if the value in A2 are text entries and not mumeric, you will need to wrap the number in quotes, i.e. change those formulas from looking like this:
Excel Formula:
A2=1
to looking like this:
Excel Formula:
A2="1"
 
Upvote 0
Solution
Code:
Public Function ValidateB2()
Select Case True
    Case Range("b2").Value = 1 And Left(Range("a2").Value, 3) = "280"
       MsgBox "OK"
    Case Range("b2").Value = 2 And Left(Range("a2").Value, 3) = "474"
       MsgBox "OK"
    Case Range("b2").Value = 3 And Left(Range("a2").Value, 3) = "860"
       MsgBox "OK"
    Case Range("b2").Value = 4 And Left(Range("a5").Value, 3) = "358"
       MsgBox "OK"
    Case Range("b2").Value = 0 And Left(Range("a2").Value, 3) = "358"
       MsgBox "OK"
    Case Else
       MsgBox "NOK"
End Select
End Function
[code]/
 
Upvote 0
Code:
Public Function ValidateB2()
Select Case True
    Case Range("b2").Value = 1 And Left(Range("a2").Value, 3) = "280"
       MsgBox "OK"
    Case Range("b2").Value = 2 And Left(Range("a2").Value, 3) = "474"
       MsgBox "OK"
    Case Range("b2").Value = 3 And Left(Range("a2").Value, 3) = "860"
       MsgBox "OK"
    Case Range("b2").Value = 4 And Left(Range("a5").Value, 3) = "358"
       MsgBox "OK"
    Case Range("b2").Value = 0 And Left(Range("a2").Value, 3) = "358"
       MsgBox "OK"
    Case Else
       MsgBox "NOK"
End Select
End Function
[code]/
No VBA is necessary. Very easy to do with Excel formulas.
 
Upvote 0
A slightly shorter option:

Excel Formula:
=IF(OR((B2&"-"&LEFT(A2,3))={"1-280","2-474","3-860","4-358","0-358"}),"OK","NOK")
 
Upvote 0
Welcome to the Board!

You can just use OR and ANDs, i.e.
Excel Formula:
=IF(OR(AND(A2=1,LEFT(B2,3)="280"),AND(A2=2,LEFT(B2,3)="474"),AND(A2=3,LEFT(B2,3)="860"),AND(A2=4,LEFT(B2,3)="358"),AND(A2=0,LEFT(B2,3)="358")),"OK","")
or more simply:
Excel Formula:
=IF(OR(AND(A2=1,LEFT(B2,3)="280"),AND(A2=2,LEFT(B2,3)="474"),AND(A2=3,LEFT(B2,3)="860"),AND(OR(A2=4,A2=0),LEFT(B2,3)="358")),"OK","")

Note that if the value in A2 are text entries and not mumeric, you will need to wrap the number in quotes, i.e. change those formulas from looking like this:
Excel Formula:
A2=1
to looking like this:
Excel Formula:
A2="1"
I had to change the A's to B's and all the , to ; -> I works perfect now. Thank you very much!
 
Upvote 0
I had to change the A's to B's and all the , to ; -> I works perfect now. Thank you very much!
Ah, sorry! I seem to have a little letter dyslexia this morning!
Anyway, glad you got it worked out!
 
Upvote 0

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