Trouble with simple If and Else

  • Thread starter Thread starter AlanN
  • Start date Start date
A

AlanN

Can someone tell me what's wrong with this? I keep getting the error "else without if"

Sub test()

Dim Msg, Style, Title

Msg = "Do you want to go ahead and continue setting up the individual store pages?"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "Individual Store Pages"
Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then GoTo Reports

Else

GoTo Endit

End If

Reports:

code

Endit:

more code

End Sub


TIA, Alan N
 
The line

If Response = vbYes Then GoTo Reports

is complete i.e. this style does not need an End If.

This makes the Else line hang without a preceding If

You also need an Exit Sub before line Endit: otherwise,
the Endit: code will be executed as well.

Cleaner code:

Select case MsgBox(Msg, Style, Title)



end select
-----Original Message-----
Can someone tell me what's wrong with this? I keep
getting the error "else without if"
 
Sub test()
Dim Msg, Style, Title
Msg = "Do you want to go ahead and continue setting" & _
" up the individual store pages?"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "Individual Store Pages"
IF MsgBox(Msg, Style, Title)= vbYes then
' code for reports here
' or call a procedure
End If

''code here will execute always

End Sub


Using GOTO's is redundant as you'd code the'Yes' result
within the 'if - end if'. If the answer is anything other
than vbYes, the code continues after the 'end if'


Patrick Molloy
Microsoft Excel MVP

-----Original Message-----
Can someone tell me what's wrong with this? I keep
getting the error "else without if"
 
Thanks, all responses are helpful.

AlanN
Can someone tell me what's wrong with this? I keep getting the error "else without if"

Sub test()

Dim Msg, Style, Title

Msg = "Do you want to go ahead and continue setting up the individual store pages?"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "Individual Store Pages"
Response = MsgBox(Msg, Style, Title)

If Response = vbYes Then GoTo Reports

Else

GoTo Endit

End If

Reports:

code

Endit:

more code

End Sub


TIA, Alan N
 
small modification
BUT SMART pls try this

PS. select case is a fast solution on the other hand it
gives you the possibility to use more options

Konrad

Sub test()

Dim Msg, Style, Title

Msg = "Do you want to go ahead and continue setting up the
individual store pages?"
Style = vbYesNo + vbCritical + vbDefaultButton2
Title = "Individual Store Pages"
Response = MsgBox(Msg, Style, Title)


Select Case Response

Case vbYes
'first code
MsgBox "vbYes"

Case vbNo
'secound code
MsgBox "vbNo"
End Select



End Sub

-----Original Message-----
Can someone tell me what's wrong with this? I keep
getting the error "else without if"
 
Back
Top