BeforeSave Userform, Cancel = don't save, OK = save

LeoExcel

New Member
Joined
Jun 15, 2005
Messages
37
Ok i ran into a problem with my userform.

When the form is canceled I want to to not save the document. Even if the document is being closed and the Save Changes? option runs, this macro runs.

My problem was that the document was saving regardless after the Userform Cancel button was used. So I added Cancel = True to my workbook code.

Original Code:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
frmCommentForm.Show
End Sub

New Code:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
frmCommentForm.Show
Cancel = True
End Sub

Userform Code:
Code:
Private Sub CancelButton_Click()
 MsgBox "If Comment History is not added, no changes will be saved.", vbCritical + vbOKOnly, "Cancel Comment"
 Unload Me
End Sub

Private Sub OkButton_Click()
 Set MySheet = Sheets("Update History")
 NextCell = Sheets("Update History").Range("a65536").End(xlUp).Row + 1
 
 MySheet.Unprotect "dstengineering"
 
 MySheet.Cells(NextCell, 1) = Environ("username")
 MySheet.Cells(NextCell, 2).Value = Now
 MySheet.Cells(NextCell, 3) = txtComments.Value

 MySheet.Protect "dstengineering"
 
Unload Me

End Sub

Now my problem is the document does not save even when a comment is added and OK is clicked. It also wont Save As or Save on Close now either with this version of the macro. Any suggestions on what to add to my OkButton_Click to get it to save properly?
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.
Not tested, but:

<font face=Tahoma><SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> CancelButton_Click()
        MsgBox "If Comment History is not added, no changes will be saved.", vbCritical + vbOKOnly, "Cancel Comment"
        Unload Me
        <SPAN style="color:#007F00">'  Close without saving changes</SPAN>
        ActiveWorkbook.Close <SPAN style="color:#00007F">False</SPAN>
<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN>

<SPAN style="color:#00007F">Private</SPAN> <SPAN style="color:#00007F">Sub</SPAN> OkButton_Click()
    <SPAN style="color:#00007F">Set</SPAN> MySheet = Sheets("Update History")
        NextCell = Sheets("Update History").Range("a65536").End(xlUp).Row + 1
        
        <SPAN style="color:#00007F">With</SPAN> MySheet
            .Unprotect "dstengineering"
                .Cells(NextCell, 1) = Environ("username")
                .Cells(NextCell, 2).Value = Now
                .Cells(NextCell, 3) = txtComments.Value
            .Protect "dstengineering"
        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN>
        
        Unload Me
        <SPAN style="color:#007F00">'   Close & Save Changes</SPAN>
        ActiveWorkbook.Close <SPAN style="color:#00007F">True</SPAN>

<SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN></FONT>

And take Cancel = True out of the BeforeSave event. That's overriding any attempt to save.

Hope that helps,

Smitty
 
Upvote 0
Thanks Smitty,

That kinda works, if its like this:
Code:
Private Sub CancelButton_Click()
        MsgBox "If Comment History is not added, no changes will be saved.", vbCritical + vbOKOnly, "Cancel Comment"
        '  Close without saving changes
        ActiveWorkbook.Close False
        Unload Me
        
End Sub

Private Sub OkButton_Click()
    Set MySheet = Sheets("Update History")
        NextCell = Sheets("Update History").Range("a65536").End(xlUp).Row + 1
        
        With MySheet
            .Unprotect "dstengineering"
                .Cells(NextCell, 1) = Environ("username")
                .Cells(NextCell, 2).Value = Now
                .Cells(NextCell, 3) = txtComments.Value
            .Protect "dstengineering"
        End With
        '   Close & Save Changes
        ActiveWorkbook.Close True

        Unload Me
        
        
End Sub
So that it exits from the userform. The problem with this is, that it closes the document regardless now. If save button or Save as is used, then the document should stay open after Userform runs, regardless of Cancel or OK button click. Only should exit the document if the Spreadsheet is closed and tries to save changes before closing.
 
Upvote 0
The problem with this is, that it closes the document regardless now. If save button or Save as is used, then the document should stay open after Userform runs, regardless of Cancel or OK button click. Only should exit the document if the Spreadsheet is closed and tries to save changes before closing.
To keep the wb from from closing, take out the ActiveWorkbook.Close statements. The True/False in the statements tells Excel whether to save or not. You can replace it with ActiveWorkbook.Save in the OK click event.

If a user selects Save or Save As, as long as you took Cancel = True out of the BeforeSave event, it should do what you want there.

I'm not really clear on the last sentence.

Smitty
 
Upvote 0
I'm not doing a very good job of explaining.

This code works almost exactly how it should, only problem is that the Userform run from someone trying to save document. If the user tries to save the document, then hits the Cancel button on this form, then i want the document to not save either.

Wrokbook:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
frmCommentForm.Show
End Sub

Userform:
Code:
Private Sub CancelButton_Click()
 MsgBox "If Comment History is not added, no changes will be saved.", vbCritical + vbOKOnly, "Cancel Comment"
Unload Me
End Sub

Private Sub OkButton_Click()
 Set MySheet = Sheets("Update History")
 NextCell = Sheets("Update History").Range("a65536").End(xlUp).Row + 1
 
 MySheet.Unprotect "dstengineering"
 
 MySheet.Cells(NextCell, 1) = Environ("username")
 MySheet.Cells(NextCell, 2).Value = Now
 MySheet.Cells(NextCell, 3) = txtComments.Value

 MySheet.Protect "dstengineering"
 
Unload Me

End Sub

I really just need something to add to the cancel button code that will stop the workbook from saving and exit the Userform. My problem with ActiveWorkbook.Close False is that this closes the workbook, even if only the save button started the macro and all changes are lost.
 
Upvote 0

Forum statistics

Threads
1,214,863
Messages
6,121,978
Members
449,058
Latest member
oculus

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top