compile error: else without if

P

papa

I have bee trying to work an example vb problem and keep
getting a compile error: else without if. I do not
understand why. Here is the code.

Private Sub CndDisplayMsgbox_Click()
Dim ButtonChoice As Integer
Dim iconchoice As Integer
Dim answer As Integer

If OptOKOnly.Value = True Then ButtonChoice = vbOKOnly
ElseIf OptOKCancel.Value = True Then ButtonChoice =
vbOKCancel
ElseIf OptAbortRetryIgnore.Value = True Then
ButtonChoice = vbAbortRetryIgnore
ElseIf OptYesNoCancel.Value = True Then
ButtonChoice = vbYesNoCancel
ElseIf OptYesNo.Value = True Then ButtonChoice = vbYesNo
ElseIf OptRetryCancel.Value = True Then ButtonChoice =
vbRetryCancel

Else: MsgBox "Unexpected Error in If statement!"
End
End If

Any ideas would be greatly appreciated.
TIA
 
F

Frank Kabel

Hi
delete the 'End' statement just before the 'End if' statement as this
will stop the macro execution
 
P

papa

That did not change anything. I don't think it is getting
past the first elseif to even worry about the end.
The following is highlighted in blue:
ElseIf OptOKCancel.Value = True Then

and the following is highlighted in yellow with the arrow
to the left:
Private Sub CndDisplayMsgbox_Click()
 
F

Frank Kabel

Hi
you also have to add a linebreak after the first Then. Try the
following:

Private Sub CndDisplayMsgbox_Click()
Dim ButtonChoice As Integer
Dim iconchoice As Integer
Dim answer As Integer

If OptOKOnly.Value = True Then 'insert linebreak here
ButtonChoice = vbOKOnly
ElseIf OptOKCancel.Value = True Then ButtonChoice = vbOKCancel
ElseIf OptAbortRetryIgnore.Value = True Then
ButtonChoice = vbAbortRetryIgnore
ElseIf OptYesNoCancel.Value = True Then
ButtonChoice = vbYesNoCancel
ElseIf OptYesNo.Value = True Then ButtonChoice = vbYesNo
ElseIf OptRetryCancel.Value = True Then ButtonChoice = vbRetryCancel

Else
MsgBox "Unexpected Error in If statement!"

End If
End Sub
 
T

Tom Ogilvy

If OptOKOnly.Value = True Then ButtonChoice = vbOKOnly
ElseIf OptOKCancel.Value = True Then ButtonChoice =
vbOKCancel

The first line is a single line if statement, so your second line is
illegal.
If OptOKOnly.Value = True Then
ButtonChoice = vbOKOnly
ElseIf OptOKCancel.Value = True Then
ButtonChoice = vbOKCancel
Elseif . . . Then
' code
Elseif . . . Then
' code
Else

End if
 
P

papa

I got further in the code that time. Thank you.
What is the single line if statement translate to? What
makes it different that splitting the statement between
two lines?
 
T

Tom Ogilvy

A single line if statement has to all be on one line

if condition then result

or

if condition then result else other result

so you can not put any part of an if statement on another line such as an
elseif or end if because they won't match up.

the compiler sees the single line if statement as a complete command.

the comiler sees a single line if statement if there is anything after the
Then statement on the same line.

from an appearance standpoint you can do

if conditition then : result
Elseif condition then : another result
else : other result
End if

by using a command separator, but this is only for appearance. From a code
organization stantpoint, it is the same as

if condition then
result
elseif condition then
another result
else
other result
end if
 
P

papa

My debugger has gotton through the first If/then/else.
However, the second one, which looks to me to be just like
the first is getting hung up.
I have pasted it below, I have used all of your feedback
but this is still stalling.
Please help.

If optCritial.Value = True Then

iconchoice = vbCritical
ElseIf OptQuestion.Value = True Then
iconchoice = vbQuestion
ElseIf OptExclamation.Value = True Then
iconchoice = vbExclamation
ElseIf OptInformation.Value = True Then
iconchoice = vbInformation
ElseIf OptNoIcon.Value = True Then
iconchoice = 0
Else: MsgBox "Abnormal Icon choice.Terminating."
End
 
J

Jeff Meeko

You have not terminated the If/Then block with the required End If
statement.
Change the last line from End to End If.

jm
 
G

Guest

I didn't get that line copied, but an end if does exist on
the next line (that I did not paste).
 
F

Frank Kabel

Hi
then delete the End statement. No reason for this statement at this
point. It will stop the macro execution.
 
P

papa

Frank,
I have removed the end and the same thing happens. It
appears that the code is not progressing beyond the first
If then statement as the following is highlighted upon the
break.

If optCritial.Value = True Then

iconchoice = vbCritical
 
J

JWolf

Option explicit?
optCritial or optCritical?
Frank,
I have removed the end and the same thing happens. It
appears that the code is not progressing beyond the first
If then statement as the following is highlighted upon the
break.

If optCritial.Value = True Then

iconchoice = vbCritical




statement at this
 
G

Guest

THAT'S IT! Thank you. It took me a minute to see the
difference even in your post. I need to learn to
transpose characters.

THANK YOU Frank and JWOLF
 
J

JWolf

Start all your modules with the line: Option Explicit or in the VBA
editor check the box under Tools, Options, Editor tab, Require Variable
Declaration and Excel will automatically insert Option Explicit in each
module. Then you must declare all variables with a Dim statement. Any
subsequent misspellings will cause a Compile error: Variable not defined
which alerts you to the misspellings. Declaring all variables may seem
like a pain and a waste of time, but after a few compile errors you will
see the value of requiring all variables to be declared.
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top