ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   If pressed no to msgbox, then exit sub (https://www.excelbanter.com/excel-programming/320946-if-pressed-no-msgbox-then-exit-sub.html)

Hari[_3_]

If pressed no to msgbox, then exit sub
 
Hi,

I have included the following 2 lines of code

msgbox ("Running of a Macro empties the UNDO stack. You should save your
workbook with a different version number before running this Macro. Press
Yes if you would you like to exit out of the macro and save your workbook.
Press No if you have already saved the workbook and would like to continue
with macro execution", vbOKCancel, "Warning") As msgboxresult
If msgboxresult = "Cancel" Then Exit Sub


When running the above code it bombs at the line - msgbox ("...") As
msgboxresult
The error im getting is -- compile error : statement invalid outside Type
Block

Please guide me as to what am doing wrong

--
Thanks a lot,
Hari
India



Edwin Tam[_7_]

If pressed no to msgbox, then exit sub
 
See the following simple example:

Sub testing()
Dim response
response = MsgBox("My message here.", vbYesNo, "My Title")
If response = vbNo Then
Exit Sub
End If
MsgBox ("You clicked YES.")
End Sub

Regards,
Edwin Tam

http://www.vonixx.com


"Hari" wrote:

Hi,

I have included the following 2 lines of code

msgbox ("Running of a Macro empties the UNDO stack. You should save your
workbook with a different version number before running this Macro. Press
Yes if you would you like to exit out of the macro and save your workbook.
Press No if you have already saved the workbook and would like to continue
with macro execution", vbOKCancel, "Warning") As msgboxresult
If msgboxresult = "Cancel" Then Exit Sub


When running the above code it bombs at the line - msgbox ("...") As
msgboxresult
The error im getting is -- compile error : statement invalid outside Type
Block

Please guide me as to what am doing wrong

--
Thanks a lot,
Hari
India




Jim Cone

If pressed no to msgbox, then exit sub
 
Hello Hari,

I changed a few things and it works.
Remember "Msgbox" is a function. If you want a return value
then you enclose the arguments in ( ), just like any other function.
Then you use a variable to store the value returned.
In this case "MsgBoxResult" is assigned the value of the button clicked.
The VBA help file is pretty good explaining messages boxes, it should help.
'-------------------------
Sub Test()
Dim MsgBoxResult As Long

MsgBoxResult = MsgBox("Running a Macro empties the UNDO stack." & vbCr & _
"You should save your workbook with a different version number before running this Macro." & vbCr & _
"Press Yes if you would like to exit out of the macro and save your workbook." & vbCr & _
"Press No if you have already saved the workbook and would like to continue with macro execution. ", _
vbYesNoCancel, " Warning")

If MsgBoxResult = vbCancel Then
Exit Sub
ElseIf MsgBoxResult = vbYes Then
'do something
Else
'do something else
End If

End Sub
'---------------------------------------
Regards,
Jim Cone
San Francisco, USA


"Hari" wrote in message
...
Hi,
I have included the following 2 lines of code
msgbox ("Running of a Macro empties the UNDO stack. You should save your
workbook with a different version number before running this Macro. Press
Yes if you would you like to exit out of the macro and save your workbook.
Press No if you have already saved the workbook and would like to continue
with macro execution", vbOKCancel, "Warning") As msgboxresult
If msgboxresult = "Cancel" Then Exit Sub
When running the above code it bombs at the line - msgbox ("...") As
msgboxresult
The error im getting is -- compile error : statement invalid outside Type
Block
Please guide me as to what am doing wrong
Thanks a lot,
Hari
India



Patrick Molloy[_2_]

If pressed no to msgbox, then exit sub
 
dim msg as string

msg = "Running of a Macro empties the UNDO stack. You should save your
workbook with a different version number before running this Macro. Press
Yes if you would you like to exit out of the macro and save your workbook.
Press No if you have already saved the workbook and would like to continue
with macro execution"

If MSGBOX( msg , vbOKCancel, "Warning") = vbCancel Then

Exit Sub
End If



"Hari" wrote:

Hi,

I have included the following 2 lines of code

msgbox ("Running of a Macro empties the UNDO stack. You should save your
workbook with a different version number before running this Macro. Press
Yes if you would you like to exit out of the macro and save your workbook.
Press No if you have already saved the workbook and would like to continue
with macro execution", vbOKCancel, "Warning") As msgboxresult
If msgboxresult = "Cancel" Then Exit Sub


When running the above code it bombs at the line - msgbox ("...") As
msgboxresult
The error im getting is -- compile error : statement invalid outside Type
Block

Please guide me as to what am doing wrong

--
Thanks a lot,
Hari
India




Hari[_3_]

If pressed no to msgbox, then exit sub
 
Hi Edwin, Jim and Patrick,

Thanx a lot for your kind help. All of your codes are working nicely for me.

Jim - Thanx a ton for introducing me to vbcr and msgbox being a function.

One doubt non-related to excel. Please tell me if possible.

IF i check for your responses in Google Im able to see Edwin's reply, but my
Outlook Express is not showing Edwin's reply (Initially it was showing as
message not available in the server etc.) Why is there a difference between
these 2 mediums

Thanks a lot,
Hari
India

"Jim Cone" wrote in message
...
Hello Hari,

I changed a few things and it works.
Remember "Msgbox" is a function. If you want a return value
then you enclose the arguments in ( ), just like any other function.
Then you use a variable to store the value returned.
In this case "MsgBoxResult" is assigned the value of the button clicked.
The VBA help file is pretty good explaining messages boxes, it should

help.
'-------------------------
Sub Test()
Dim MsgBoxResult As Long

MsgBoxResult = MsgBox("Running a Macro empties the UNDO stack." & vbCr & _
"You should save your workbook with a different version number before

running this Macro." & vbCr & _
"Press Yes if you would like to exit out of the macro and save your

workbook." & vbCr & _
"Press No if you have already saved the workbook and would like to

continue with macro execution. ", _
vbYesNoCancel, " Warning")

If MsgBoxResult = vbCancel Then
Exit Sub
ElseIf MsgBoxResult = vbYes Then
'do something
Else
'do something else
End If

End Sub
'---------------------------------------
Regards,
Jim Cone
San Francisco, USA


"Hari" wrote in message
...
Hi,
I have included the following 2 lines of code
msgbox ("Running of a Macro empties the UNDO stack. You should save your
workbook with a different version number before running this Macro.

Press
Yes if you would you like to exit out of the macro and save your

workbook.
Press No if you have already saved the workbook and would like to

continue
with macro execution", vbOKCancel, "Warning") As msgboxresult
If msgboxresult = "Cancel" Then Exit Sub
When running the above code it bombs at the line - msgbox ("...") As
msgboxresult
The error im getting is -- compile error : statement invalid outside

Type
Block
Please guide me as to what am doing wrong
Thanks a lot,
Hari
India





Jim Cone

If pressed no to msgbox, then exit sub
 
Hari,

"Why is there a difference between these 2 mediums"

I can't help you there.

Regards,
Jim Cone

"Hari" wrote in message
...
Hi Edwin, Jim and Patrick,

Thanx a lot for your kind help. All of your codes are working nicely for me.
Jim - Thanx a ton for introducing me to vbcr and msgbox being a function.
One doubt non-related to excel. Please tell me if possible.
IF i check for your responses in Google Im able to see Edwin's reply, but my
Outlook Express is not showing Edwin's reply (Initially it was showing as
message not available in the server etc.) Why is there a difference between
these 2 mediums
Thanks a lot,
Hari
India




All times are GMT +1. The time now is 10:57 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
ExcelBanter.com