Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 157
Default 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


  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 94
Default 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



  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default 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


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 157
Default 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




  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default 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




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,298
Default 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



Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
Which button was pressed? Adrian[_4_] Excel Programming 7 April 28th 04 04:55 PM
VB msgbox exit Fraggs[_3_] Excel Programming 2 April 10th 04 02:00 PM
If a called sub exit, how to the caller exit right away? luvgreen[_4_] Excel Programming 4 February 24th 04 05:06 PM
ComboBox DropDown when Key is pressed shrekut[_9_] Excel Programming 2 February 2nd 04 11:30 PM
If no FIND value is found, then a Msgbox to say so and exit sub L. Howard Kittle[_2_] Excel Programming 3 September 23rd 03 08:46 PM


All times are GMT +1. The time now is 05:42 PM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"