# Correct IF statement

#### Floki

##### New Member
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

How to create a cell-sized chart?
Tiny charts, called Sparklines, were added to Excel 2010. Look for Sparklines on the Insert tab.

#### Jonmo1

##### MrExcel MVP
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

#### AlphaFrog

##### MrExcel MVP
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]``````

#### Floki

##### New Member
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

Replies
2
Views
251
Replies
3
Views
228
Replies
0
Views
637
Replies
1
Views
716
Replies
4
Views
198

1,195,828
Messages
6,011,829
Members
441,650
Latest member
ceyoung75

### 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.

### Which adblocker are you using?

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

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