How to cancel save if chosen with BeforeSave?

postelrich

New Member
Joined
Feb 24, 2011
Messages
31
I have made a userform that popsup when someone saves the file that asks them to put in some comments and copies that to a changelog in the workbook. Everything works fine except for the cancel button which will still save if chosen. I have tried searching and a bunch of methods. Here is what I have so far.

Code for the event:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
 
UserForm1.Show
 
If CancelIt = True Then
    Cancel = True
End If
 
Unload UserForm1
 
End Sub

Code for the Userform:
Code:
Public CancelIt As Boolean
 
Private Sub CommandButton1_Click()
 
Dim comments As String
Dim LastRow
comments = TextBox1.Value
 
With Sheets("Changelog & Reference")
     LastRow = .UsedRange.Rows.Count + 1
    .Range("A" & LastRow).Value = Now
    .Range("B" & LastRow).Value = Environ("USERNAME")
    .Range("C" & LastRow).Value = comments
End With
 
Unload UserForm1
End Sub
 
Private Sub CommandButton2_Click()
 
CancelIt = True
UserForm1.Hide
 
End Sub
 
Last edited:

Excel Facts

How to find 2nd largest value in a column?
MAX finds the largest value. =LARGE(A:A,2) will find the second largest. =SMALL(A:A,3) will find the third smallest
That didn't help. I added it to the top of both as well as had to declare the CancelIt variable in the ThisWorkbook code.

Code:
Option Explicit
Public CancelIt As Boolean
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
UserForm1.Show
If CancelIt = True Then
    Cancel = True
End If
Unload UserForm1
    
End Sub
 
Upvote 0
Just for verification,what do you mean by still saving?
Does it show a SaveAs Dialog? or just saves?

How is the document saved? Is it through VBA or just user-input?
 
Upvote 0
What I have done is when someone hits the save or saveas button in Excel (not VBA) the userform will popup. It has both an OK button and a Cancel button. I want it so when they hit cancel, it doesn't save. But right now when I choose Cancel it will still save (or go to the SaveAs dialog if SaveAs was chosen).
 
Upvote 0

Forum statistics

Threads
1,224,517
Messages
6,179,242
Members
452,898
Latest member
Capolavoro009

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