VBA compile error else without if

helium

New Member
Joined
Jan 17, 2012
Messages
14
Hi guys,

I'm new at writing vba and am trying to get this user form working but keep getting the compile error: Else without if.

Code:
Private Sub cmdOK_Click()
    ActiveWorkbook.Sheets("Course Bookings").ActivateRange("A1").Select
    Do
    If IsEmpty(ActiveCell) = False Then
        ActiveCell.Offset(1, 0).Select
    End If
    Loop Until IsEmpty(ActiveCell) = True
    ActiveCell.Value = txtName.Value
    ActiveCell.Offset(0, 1) = txtPhone.Value
    ActiveCell.Offset(0, 2) = cboDepartment.Value
    ActiveCell.Offset(0, 3) = cboCourse.Value
    If optIntroduction = True Then ActiveCell.Offset(0, 4).Value = "Intro"
    [B]ElseIf optIntermediate = True Then[/B]
            ActiveCell.Offset(0, 4).Value = "Intermd"
                Else
                ActiveCell.Offset(0, 4).Value = "Adv"
                End If
                If chkLunch = True Then
                    ActiveCell.Offset(0, 5).Value = "Yes"
                Else
                    ActiveCell.Offset(0, 5).Value = "No"
                End If
                If chkVegetarian = True Then
                ActiveCell.Offset(0, 6).Value = "Yes"
                Else
                    If chkLunch = False Then
                    ActiveCell.Offset(0, 6).Value = ""
                    Else
                    ActiveCell.Offset(0, 6).Value = "No"
                End If
                Range("A1").Select

The bolded text is what the editor highlights. What am I going wrong?
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
This a complete If statement.
Code:
If optIntroduction = True Then ActiveCell.Offset(0, 4).Value = "Intro"
Anything after it isn't part of the If statement.

Try this.
Code:
If optIntroduction = True Then 
       ActiveCell.Offset(0, 4).Value = "Intro"
ElseIf  optIntermediate = True Then
       ActiveCell.Offset(0, 4).Value = "Intermd"
Else
       ActiveCell.Offset(0, 4).Value = "Adv"
End If
 
Upvote 0
Thanks Norie!

However, now I'm getting a different error: Block If without End If. The new code I'm trying is...
Code:
Private Sub cmdOK_Click()
    ActiveWorkbook.Sheets("Course Bookings").ActivateRange("A1").Select
Do
If IsEmpty(ActiveCell) = False Then
    ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
    ActiveCell.Value = txtName.Value
    ActiveCell.Offset(0, 1) = txtPhone.Value
    ActiveCell.Offset(0, 2) = cboDepartment.Value
    ActiveCell.Offset(0, 3) = cboCourse.Value
If optIntroduction = True Then
       ActiveCell.Offset(0, 4).Value = "Intro"
ElseIf optIntermediate = True Then
       ActiveCell.Offset(0, 4).Value = "Intermd"
Else
       ActiveCell.Offset(0, 4).Value = "Adv"
End If
    If chkLunch = True Then
        ActiveCell.Offset(0, 5).Value = "Yes"
    Else
        ActiveCell.Offset(0, 5).Value = "No"
    End If
    If chkVegetarian = True Then
        ActiveCell.Offset(0, 6).Value = "Yes"
    Else
    If chkLunch = False Then
        ActiveCell.Offset(0, 6).Value = ""
    Else
        ActiveCell.Offset(0, 6).Value = "No"
    End If
        Range("A1").Select
[B]End Sub[/B]

It now highlights "End Sub."

Sorry completely new at vba trying to debug code I got from someone.
 
Upvote 0
Proper indentation of code will help discover these issues.
You were missing the Red End If

Rich (BB code):
Private Sub cmdOK_Click()
ActiveWorkbook.Sheets("Course Bookings").ActivateRange("A1").Select
Do
    If IsEmpty(ActiveCell) = False Then
        ActiveCell.Offset(1, 0).Select
    End If
Loop Until IsEmpty(ActiveCell) = True
 
ActiveCell.Value = txtName.Value
ActiveCell.Offset(0, 1) = txtPhone.Value
ActiveCell.Offset(0, 2) = cboDepartment.Value
ActiveCell.Offset(0, 3) = cboCourse.Value
 
If optIntroduction = True Then
    ActiveCell.Offset(0, 4).Value = "Intro"
ElseIf optIntermediate = True Then
    ActiveCell.Offset(0, 4).Value = "Intermd"
Else
    ActiveCell.Offset(0, 4).Value = "Adv"
End If
 
If chkLunch = True Then
    ActiveCell.Offset(0, 5).Value = "Yes"
Else
    ActiveCell.Offset(0, 5).Value = "No"
End If
 
If chkVegetarian = True Then
    ActiveCell.Offset(0, 6).Value = "Yes"
Else
    If chkLunch = False Then
        ActiveCell.Offset(0, 6).Value = ""
    Else
        ActiveCell.Offset(0, 6).Value = "No"
    End If
End If
Range("A1").Select
End Sub
 
Upvote 0
Thanks Jonmo1,

No longer coming up with that error.. But now it is coming with 'Runtime erorr 9 subscript out of range'
 
Upvote 0
Don't worry, fixed it!

I changed
Code:
ActiveWorkbook.Sheets("Course Bookings").ActivateRange("A1").Select

to..
Code:
    Application.Goto (ActiveWorkbook.Sheets("Course Bookings").Range("A1"))

The whole code is now..

Code:
Private Sub cmdOK_Click()
    'ActiveWorkbook.Sheets("Course Bookings").ActivateRange("A1").Select
    Application.Goto (ActiveWorkbook.Sheets("Course Bookings").Range("A1"))
Do
If IsEmpty(ActiveCell) = False Then
    ActiveCell.Offset(1, 0).Select
End If
Loop Until IsEmpty(ActiveCell) = True
    ActiveCell.Value = txtName.Value
    ActiveCell.Offset(0, 1) = txtPhone.Value
    ActiveCell.Offset(0, 2) = cboDepartment.Value
    ActiveCell.Offset(0, 3) = cboCourse.Value
If optIntroduction = True Then
       ActiveCell.Offset(0, 4).Value = "Intro"
ElseIf optIntermediate = True Then
       ActiveCell.Offset(0, 4).Value = "Intermd"
Else
       ActiveCell.Offset(0, 4).Value = "Adv"
End If
    If chkLunch = True Then
        ActiveCell.Offset(0, 5).Value = "Yes"
    Else
        ActiveCell.Offset(0, 5).Value = "No"
    End If
    If chkVegetarian = True Then
        ActiveCell.Offset(0, 6).Value = "Yes"
    Else
    If chkLunch = False Then
        ActiveCell.Offset(0, 6).Value = ""
    Else
        ActiveCell.Offset(0, 6).Value = "No"
    End If
        Range("A1").Select
    End If
End Sub

Cheers for your help
 
Upvote 0

Forum statistics

Threads
1,223,315
Messages
6,171,410
Members
452,399
Latest member
oranges

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