View Single Post
  #7   Report Post  
Posted to microsoft.public.excel.programming
Tom Ogilvy Tom Ogilvy is offline
external usenet poster
 
Posts: 27,285
Default compile error: else without if

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

--
Regards,
Tom Ogilvy



"papa" wrote in message
...
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?


-----Original Message-----
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

--
Regards,
Tom Ogilvy



"papa" wrote in message
...
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()


-----Original Message-----
Hi
delete the 'End' statement just before the 'End if'
statement as this
will stop the macro execution

--
Regards
Frank Kabel
Frankfurt, Germany


papa wrote:
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

.



.