PDA

View Full Version : Exit MsgBox If MsgBoxResult = Cancel



nirvehex
11-28-2014, 11:38 AM
Hi, I have a code which I'm trying to get to exit the sub if the user clicks cancel on the message box or activate a different worksheet if they click ok.

For some reason it activates the sheet no matter what button I press.



Sub ScopeofWorkInstructions()


MsgBox " SCOPE OF WORK INSTRUCTIONS:" _
& vbNewLine & " " _
& vbNewLine & "1. Please fill out as much information as possible." _
& vbNewLine & " " _
& vbNewLine & "2. Use the Notes column for additional information discussed." _
& vbNewLine & " " _
& vbNewLine & "3. Download the Client's Logo and insert it as a picture sized to fit within the logo box." _
& vbNewLine & " " _
& vbNewLine & "4. Please return to the Navigation and check the completion box when finished." _
, vbOKCancel
If MsgBoxResult = vbCancel Then
Exit Sub
Else
Set ws = Worksheets("Scope of Work")
ws.Activate
End If


End Sub


Any idea why?

ashleyuk1984
11-28-2014, 01:44 PM
Because your message box isn't using "MsgBoxResult".
So therefore it's "Empty". Which will result in the ELSE tag.

This will fix it.


Sub ScopeofWorkInstructions()



MsgBoxResult = MsgBox(" SCOPE OF WORK INSTRUCTIONS:" _
& vbNewLine & " " _
& vbNewLine & "1. Please fill out as much information as possible." _
& vbNewLine & " " _
& vbNewLine & "2. Use the Notes column for additional information discussed." _
& vbNewLine & " " _
& vbNewLine & "3. Download the Client's Logo and insert it as a picture sized to fit within the logo box." _
& vbNewLine & " " _
& vbNewLine & "4. Please return to the Navigation and check the completion box when finished." _
, vbOKCancel)


If MsgBoxResult = vbCancel Then
Exit Sub
Else
Set ws = Worksheets("Scope of Work")
ws.Activate
End If