• Hi All

    Please note that at the Chandoo.org Forums there is Zero Tolerance to Spam

    Post Spam and you Will Be Deleted as a User

    Hui...

  • When starting a new post, to receive a quicker and more targeted answer, Please include a sample file in the initial post.

VBA Userform Save or Not Save on Exit

When executing the following code and selecting "Yes" it works as intended it saves the userform and displays the confirmation message. However, when selecting "No" it unloads the form without displaying the message.

Code:
'Exit Form Button'
Private Sub FormExit_Click()
Dim MsgBoxResult As Long
MsgBoxResult = MsgBox("Save Changes?", vbYesNo)
    If MsgBoxResult = vbYes Then
        ActiveWorkbook.Save
        MsgBox ("Your work is saved. Have a great day!")
     ElseIf MsgBoxResults = vbNo Then
        MsgBox ("Your work was NOT saved. See ya!")
    End If
Unload MasterForm
End Sub

Thank you in advance for your consideration!
 
Yodelayheewho
Displays a message in a dialog box, waits for the user to click a button, and returns an Integer indicating which button the user clicked.
I would use if ... then ... else ... endif - syntax.

Code:
Private Sub FormExit_Click()
    Dim MsgBoxResult As Integer
    Dim txt As String
    If MsgBox("Save Changes?", vbYesNo) = vbYes Then
        ActiveWorkbook.Save
        txt = "Your work is saved. Have a great day!"
     Else
        txt = "Your work was NOT saved. See ya!"
    End If
    MsgBox txt
    Unload MasterForm
End Sub
 
Perfection! Thank you so very much vletm!
One question. You have Dim MsgBox Result As Integer. The result is not a number it is text. How does this work?
 
Last edited:
Yodelayheewho
Seems that I skipped to delete that Dim MsgBoxResult As Integer -line.
Your above writing has anyway error ... MsgBox Result.
Did You check that my the 1st link? ... please, recheck that.
... then You'll notice that MsgBox gives integer - You can verify that Yourself too while running that code.
Instead numbers, there can use vbYes,vbNo and many others.
 
I did read the articles and saved them for future reference. I learned some new things today.
Here is the updated code and it works great. Thanks again vletm!

Code:
'Exit Form Button'
Private Sub FormExit_Click()
Dim txt As String
If MsgBox("Save Changes?", vbYesNo) = vbYes Then
        ActiveWorkbook.Save
        txt = "Your work is saved. Have a great day!"
     Else
        txt = "Your work was NOT saved. See ya!"
    End If
    MsgBox txt
Unload MasterForm
End Sub
 
Back
Top