Issues with code to prevent saving changes on close

davidam

Active Member
Joined
May 28, 2010
Messages
497
Office Version
  1. 2021
Platform
  1. Windows
Hello All,
Long ago, I copied the following code into the Workbook Objects.

VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
ActiveWorkbook.Saved = True
Cancel = True
End Sub

It has always worked fine, it seems. However, I sometimes have some kind of memory leak that occurs when I close other workbooks while this workbook is open. I wonder if anyone can comment
on the variables that are bracketed in the above code, which seem to have no purpose that I can find.
Thanks,
David
 

Excel Facts

Fastest way to copy a worksheet?
Hold down the Ctrl key while dragging tab for Sheet1 to the right. Excel will make a copy of the worksheet.
Maybe:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    Dim WB As Workbook
    Set WB = ThisWorkbook
    WB.Saved = True
    Cancel = True
End Sub
 
Upvote 0
Right. I think ThisWorkbook definitely makes more sense than ActiveWorkbook. I still don't understand the variables.
 
Upvote 0
I suppose that you could shorten it to:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    ThisWorkbook.Saved = True
    Cancel = True
End Sub
 
Upvote 0
Agreed. Any thoughts on the bracketed variables? Like I said, I copied this code from another source and I really cannot understand what those variables are for.
P.S. I do think the change that you suggested could be a factor in the 'Out of Memory' issue.
 
Upvote 0
Great. I guess my confusion arises from my understanding of passing variables from one sub to another. Am I correct to understand that in a worksheet event like this, I am supposed to address the variables within the event sub. In the case of the 'Cancel', I am setting it to 'True'. Would the following be correct if I wanted to prevent the UI display?
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    ThisWorkbook.Saved = True
    SaveAsUI = False
    Cancel = True
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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