+ Reply to Thread
Results 1 to 7 of 7

VBA userform with a message prompt to save or exit without saving

  1. #1
    Registered User
    Join Date
    12-03-2014
    Location
    Scotland
    MS-Off Ver
    2013
    Posts
    11

    VBA userform with a message prompt to save or exit without saving

    Hello

    I've created a VBA program and have included an EXIT button on a userform. I expect the EXIT button to prompt the user with the message "Do you wish to save the changes to this workbook" to confirm press YES or NO. If the user clicks YES then the program is saved and closed. If the user clicks NO the program/excel is closed down without saving.

    This is what I have so far:


    Private Sub CommandButton3_Click()
    Dim MsgBoxResult As Long

    MsgBoxResult = MsgBox("Do you wish to save the changes to this workbook" & vbCr & _
    vbYesNoCancel)

    If MsgBoxResult = vbCancel Then

    Application.DisplayAlerts = False
    Application.Quit

    ElseIf MsgBoxResult = vbYes Then
    ThisWorkbook.Save
    Application.DisplayAlerts = False

    End If
    End Sub



    When I click the EXIT button, the message pops up with an OK button but not a YES and NO button.

    Any ideas??? Any help would be great!

    Thanks all

  2. #2
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: VBA userform with a message prompt to save or exit without saving

    You have concatenated the parameters for the MsgBox. They should be separated with commas. Look at the Help for Msgbox ... or just start typing to see the prompts. When you type a comma, it will move on to the next one.

    Regards, TMS
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  3. #3
    Registered User
    Join Date
    12-03-2014
    Location
    Scotland
    MS-Off Ver
    2013
    Posts
    11

    Re: VBA userform with a message prompt to save or exit without saving

    Quote Originally Posted by TMS View Post
    You have concatenated the parameters for the MsgBox. They should be separated with commas. Look at the Help for Msgbox ... or just start typing to see the prompts. When you type a comma, it will move on to the next one.

    Regards, TMS
    Thanks for your response.

    I've tried rewriting the code following the prompts as you said but couldn't get it to work...

    Do you have an example?

    Thanks

  4. #4
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: VBA userform with a message prompt to save or exit without saving

    Please note ...

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    Please Login or Register  to view this content.


    Regards, TMS

  5. #5
    Forum Expert
    Join Date
    07-31-2010
    Location
    California
    MS-Off Ver
    Excel 2007
    Posts
    4,070

    Re: VBA userform with a message prompt to save or exit without saving

    TMS was referring to this;

    Please Login or Register  to view this content.
    These are the parameters of a msgbox. Notice the commas. They must remain to delineate the different parameters

  6. #6
    Registered User
    Join Date
    12-03-2014
    Location
    Scotland
    MS-Off Ver
    2013
    Posts
    11

    Re: VBA userform with a message prompt to save or exit without saving

    Quote Originally Posted by TMS View Post
    Please note ...

    Your post does not comply with Rule 3 of our Forum RULES. Use code tags around code.

    Posting code between [CODE]Please [url=https://www.excelforum.com/login.php]Login or Register [/url] to view this content.[/CODE] tags makes your code much easier to read and copy for testing, it also maintains VBA formatting.

    Highlight your code and click the # icon at the top of your post window. More information about these and other tags can be found here



    Please Login or Register  to view this content.


    Regards, TMS
    That's great - it works perfectly. Thank you very much!!

    Thanks stnkynts for your input and explanation as well.

    Great forum!

  7. #7
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,425

    Re: VBA userform with a message prompt to save or exit without saving

    You're welcome.



    If you are satisfied with the solution(s) provided, please mark your thread as Solved.


    New quick method:
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

    Or you can use this way:

    How to mark a thread Solved
    Go to the first post
    Click edit
    Click Go Advanced
    Just below the word Title you will see a dropdown with the word No prefix.
    Change to Solved
    Click Save


    You may also want to consider thanking those people who helped you by clicking on the little star at the bottom left of their reply to your question.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Don't prompt to save on exit (BeforeClose & Add-In)
    By chrisignm in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-30-2014, 04:19 PM
  2. [SOLVED] Exit Userform TextBox using message box
    By Stratfordoaks in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 11-24-2013, 01:34 PM
  3. Replies: 1
    Last Post: 02-24-2006, 10:15 PM
  4. [SOLVED] Excel - Prompt to save on exit.
    By Fenris in forum Excel General
    Replies: 0
    Last Post: 11-21-2005, 01:15 PM
  5. Prompt message and Automated saving!
    By SU in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 02-25-2005, 09:06 PM

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