Correct IF statement

Floki

New Member
Joined
May 18, 2015
Messages
14
I'm having some problems with this IF paragraph -- I could break it down, but that's not efficient. What the best way to code this - it's in a UDF that I'm trying to work on.

If Cond = "BCAT" Or Cond = "BA/M" And QF >= 0 And QF < 3 Then
Per = 0.4
ElseIf Cond = "BCAT" Or Cond = "BA/M" And QF > 4 And QF < 10 Then
Per = 0.35
ElseIf Cond = "BCAT" Or Cond = "BA/M" And QF > 9 And QF < 15 Then
Per = 0.3
ElseIf Cond = "BCAT" Or Cond = "BA/M" And QF > 14 And QF < 20 Then
Per = 0.25
ElseIf Cond = "BCAT" Or Cond = "BA/M" And QF > 19 And QF < 26 Then
Per = 0.2
ElseIf Cond = "BCAT" Or Cond = "BA/M" And QF > 25 Then
Per = 0.15
End If

Floki
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
You probably need to be more inclusive with your ranges..
If Cond = "BCAT" Or Cond = "BA/M" And QF >= 0 And QF < 3 Then
ElseIf Cond = "BCAT" Or Cond = "BA/M" And QF > 4 And QF < 10 Then

What if QF = exactly 3 or 4 ? Both of those statements would be false.

You probably want something like
If Cond = "BCAT" Or Cond = "BA/M" And QF >= 0 And QF <= 3 Then
ElseIf Cond = "BCAT" Or Cond = "BA/M" And QF >= 4 And QF <= 10 Then


Also, you may need (parens) to clarify which parts belong to which or/and..
Do you want
If (Cond = "BCAT" Or Cond = "BA/M") And (QF >= 0 And QF < 3) Then
or
If Cond = "BCAT" Or (Cond = "BA/M" And QF >= 0 And QF < 3) Then
 
Upvote 0
You could 1st test for If Cond = "BCAT" Or Cond = "BA/M" Then

Code:
    [COLOR=darkblue]If[/COLOR] Cond = "BCAT" [COLOR=darkblue]Or[/COLOR] Cond = "BA/M" [COLOR=darkblue]Then[/COLOR]
        [COLOR=darkblue]If[/COLOR] QF >= 0 And QF < 3 [COLOR=darkblue]Then[/COLOR]
            Per = 0.4
        [COLOR=darkblue]ElseIf[/COLOR] QF > 4 And QF < 10 [COLOR=darkblue]Then[/COLOR]
            Per = 0.35
        [COLOR=darkblue]ElseIf[/COLOR] QF > 9 And QF < 15 [COLOR=darkblue]Then[/COLOR]
            Per = 0.3
        [COLOR=darkblue]ElseIf[/COLOR] QF > 14 And QF < 20 [COLOR=darkblue]Then[/COLOR]
            Per = 0.25
        [COLOR=darkblue]ElseIf[/COLOR] QF > 19 And QF < 26 [COLOR=darkblue]Then[/COLOR]
            Per = 0.2
        [COLOR=darkblue]ElseIf[/COLOR] QF > 25 [COLOR=darkblue]Then[/COLOR]
            Per = 0.15
        [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
    [COLOR=darkblue]End[/COLOR] [COLOR=darkblue]If[/COLOR]
 
Upvote 0
AlphaFrog,

Thanks for your help -- it worked perfectly. Just finished up the rest of the coding and did testing for all the different conditions -- work great now due to your help.

Floki
 
Upvote 0

Forum statistics

Threads
1,214,918
Messages
6,122,255
Members
449,075
Latest member
staticfluids

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