+ Reply to Thread
Results 1 to 6 of 6

If pressed no to msgbox, then exit sub

  1. #1
    Hari
    Guest

    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. #2
    Edwin Tam
    Guest

    RE: 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
    [email protected]
    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. #3
    Jim Cone
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > 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. #4
    Patrick Molloy
    Guest

    RE: 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
    >
    >
    >


  5. #5
    Hari
    Guest

    Re: 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" <[email protected]> wrote in message
    news:[email protected]...
    > 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" <[email protected]> wrote in message
    > news:[email protected]...
    > > 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

    >




  6. #6
    Jim Cone
    Guest

    Re: If pressed no to msgbox, then exit sub

    Hari,

    Re: "Why is there a difference between these 2 mediums"

    I can't help you there.

    Regards,
    Jim Cone

    "Hari" <[email protected]> wrote in message
    news:[email protected]...
    > 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



+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1