Simple VBA Else Without If

jgodoyle2

New Member
Joined
Feb 21, 2011
Messages
2
Some simple VBA questions:

This is for a Quantitative Finance class, we have only been introduced to VBA for Excel, so this isn't hard and I can't see for the life of me what I'm doing wrong.

First of all, when I paste an example from the text into VBA I get a "Compile Error: Else without If" error:

Function gpa(s)
If s < 75 Then
gpa = 2
Else If s < 88 Then
gpa = 3
Else
gpa = 4
End If
End Function

This is supposed to be correct as per the text so maybe I need to change a setting? The function I am to write for homework looks correct following the book's instructions:

Function fedtax(x)
If x < 7300 Then fedtax = 0.1 * x
ElseIf x < 29700 Then fedtax = 730 + 0.15 * (x - 7300)
ElseIf x < 71950 Then fedtax = 4090 + 0.25 * (x - 29700)
ElseIf x < 150150 Then fedtax = 14652.5 + 0.28 * (x - 71950)
ElseIf x < 326450 Then fedtax = 36548.5 + 0.33 * (x - 150150)
Else: fedtax = 94727.5 + 0.35 * (x - 326450)
End Function

if I write a very simple if function, it turns out fine:

Function fedtax(x)
If x < 7300 Then fedtax = 0.1 * x
End If
End Function

I also tried the 2nd function using Else: If instead of ElseIf but got the same error. I appreciate any help on the matter, thanks for reading!
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Code:
[COLOR=red]ElseIf[/COLOR] s < 88 Then

This is an in-line If; only a block If gets an End If statement
Code:
[COLOR=black][FONT=Verdana]Function fedtax(x)[/FONT][/COLOR]
[FONT=Verdana][COLOR=black]If x < 7300 Then fedtax = 0.1 * x[/COLOR][/FONT]
[FONT=Verdana][COLOR=black]<S>[COLOR=red]End If[/COLOR]</S>[/COLOR][/FONT]
[COLOR=black][FONT=Verdana]End Function[/FONT][/COLOR]
Code:
Function fedtax(x)
    If x < 7300 Then
        fedtax = 0.1 * x
    ElseIf x < 29700 Then
        fedtax = 730 + 0.15 * (x - 7300)
    ElseIf x < 71950 Then
        fedtax = 4090 + 0.25 * (x - 29700)
    ElseIf x < 150150 Then
        fedtax = 14652.5 + 0.28 * (x - 71950)
    ElseIf x < 326450 Then
        fedtax = 36548.5 + 0.33 * (x - 150150)
    Else
        fedtax = 94727.5 + 0.35 * (x - 326450)
    End If
End Function
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,518
Messages
6,179,261
Members
452,901
Latest member
LisaGo

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