Compile error: Else without If

Massey1337

New Member
Joined
Mar 8, 2009
Messages
17
Hey I'm a new programmer and new to this forum, so hi, and I need help with some of my coding for a simple fortune teller program.
This is my coding:
Dim Text1 As Integer
Dim Text2 As Integer
Dim Text3 As Integer
Private Sub Command1_Click()
If Month = 1 - 4 Then MsgBox ("It's looking good")
ElseIf Month = 5 - 8 Then MsgBox ("Don't do it!")
ElseIf Month = 9 - 12 Then MsgBox ("You're doomed!")
Else
MsgBox ("Please enter a valid date of birth")
End If
End Sub
Private Sub Form_Load()
Day.Text = ""
Month.Text = ""
Year.Text = ""
End Sub
The error as stated in the title is Compile error: Else without if
I need help a.s.a.p. with this as its for college, thanks in advance.
Elliot.
 

Excel Facts

Did you know Excel offers Filter by Selection?
Add the AutoFilter icon to the Quick Access Toolbar. Select a cell containing Apple, click AutoFilter, and you will get all rows with Apple
Hello and welcome to MrExcel.

First don't use Month as a variable name - it is a VBA function.

Try

Code:
Private Sub Command1_Click()
Select Case MyMonth
    Case 1 To 4: MsgBox ("It's looking good")
    Case 5 To 8: MsgBox ("Don't do it!")
    Case 9 To 12: MsgBox ("You're doomed!")
    Case Else: MsgBox ("Please enter a valid date of birth")
End Select
End Sub
 
Upvote 0
try it like this

Code:
Dim Text1 As Integer
Dim Text2 As Integer
Dim Text3 As Integer
Private Sub Command1_Click()
If Month = 1 - 4 Then
MsgBox ("It's looking good")
Else
If Month = 5 - 8 Then
MsgBox ("Don't do it!")
Else
If Month = 9 - 12 Then MsgBox ("You're doomed!")
Else
MsgBox ("Please enter a valid date of birth")
End If
End Sub
Private Sub Form_Load()
Day.Text = ""
Month.Text = ""
Year.Text = ""
End Sub
 
Upvote 0
Thanks for the swift replies, I've been asked to use a Select Case statement like what VoG posted, however, as I try your code I'm getting "Please enter a valid date of birth." which leads me to thinking the 1 to 4 and the 5 to 8 parts are wrong?
I'm trying to change them around now, but I'm struggling any ideas on the new problem?
Thanks. :)
P.S. Thanks for your reply also Diablo that worked for me, but as I stated I need to use a Select Case statement.
 
Upvote 0
Aha, sorry, false alarm, just changing my Month variable to MyMonth worked, I think I must of typo'd, thanks so much for your help. :)
I look forward to staying at this forum. :)
 
Upvote 0

Forum statistics

Threads
1,203,115
Messages
6,053,598
Members
444,674
Latest member
DWriter9

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