I need help with my macro using a mathematical equation incorporating if else statements

Jedit

New Member
Joined
Feb 8, 2013
Messages
27
I am attempting to recollect my college programming courses to remember my coding and I am stumped by my if else statement. I have posted the code below:<o:p> </o:p>
Sub CalculateAQI()
'
'
'User inputs a numerical value located in cell (1,6) and then runs this macro
Cells(1, 6).Value = I
If 0 <= I <= 15.4 Then
IH = 50 And IL = 0 And BPH = 12 And BPL = 0
Else
If 15.5 <= I <= 40.4 Then
IH = 100 And IL = 51 And BPH = 35.4 And BPL = 12.1
Else
If 40.5 <= I <= 65.4 Then
IH = 150 And IL = 101 And BPH = 55.4 And BPL = 35.5
Else
If 65.5 <= I <= 150.4 Then
IH = 200 And IL = 151 And BPH = 150.4 And BPL = 55.5
Else
If 150.5 <= I <= 250.4 Then
IH = 300 And IL = 201 And BPH = 250.4 And BPL = 150.5
Else
If 250.5 <= I <= 350.4 Then
IH = 400 And IL = 301 And BPH = 650 And BPL = 250.5
Else
If 350.5 <= I <= 500.4 Then
IH = 500 And IL = 401 And BPH = 650 And BPL = 250.5
End If
'The destination of the final numerical value should be placed in Cell(3,6)
Cells(3, 6).Value = [(IH - IL)/(BPH -BPL)] * (I - BPL) + IL
'
End Sub
<o:p> </o:p>

There is an error once you reach the end sub saying that Idid not end the If statement which I thought I did but if someone could assistme, I would greatly appreciate it.<o:p></o:p>

Jedit<o:p></o:p>
 
you need to close out every if statement that you have listed; the way you have it done is not the best way to do it - nested if statements are a pain to maintain. If you choose to go this route, then you need to start spacing your statements so you can easily see where any gaps are.

Code:
Sub CalculateAQI()
Dim I As Integer, IL As Integer, BPH As Integer, BPL As Integer, IH As Integer
'
'
'User inputs a numerical value located in cell (1,6) and then runs this macro
Cells(1, 6).Value = I

If 0 <= I <= 15.4 Then
    IH = 50 And IL = 0 And BPH = 12 And BPL = 0
    Else
        If 15.5 <= I <= 40.4 Then
            IH = 100 And IL = 51 And BPH = 35.4 And BPL = 12.1
            Else
                If 40.5 <= I <= 65.4 Then
                    IH = 150 And IL = 101 And BPH = 55.4 And BPL = 35.5
                    Else
                        If 65.5 <= I <= 150.4 Then
                            IH = 200 And IL = 151 And BPH = 150.4 And BPL = 55.5
                            Else
                                If 150.5 <= I <= 250.4 Then
                                    IH = 300 And IL = 201 And BPH = 250.4 And BPL = 150.5
                                    Else
                                        If 250.5 <= I <= 350.4 Then
                                            IH = 400 And IL = 301 And BPH = 650 And BPL = 250.5
                                            Else
                                                If 350.5 <= I <= 500.4 Then
                                                    IH = 500 And IL = 401 And BPH = 650 And BPL = 250.5
                                                End If
                                        End If
                                End If
                        End If
                End If
        End If
End If

'The destination of the final numerical value should be placed in Cell(3,6)
Cells(3, 6).Value = [(IH - IL)/(BPH -BPL)] * (I - BPL) + IL
'
End Sub
 
Upvote 0

Similar threads

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