Prevent saving workbook

wmtsub

Active Member
Joined
Jun 20, 2018
Messages
322
So I thought if I placed this code into the Workbook Module it would prevent the workbook from being saved or saved as. What did I miss?


Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Application.DisplayAlerts = False
ThisWorkbook.Saved = True
Application.DisplayAlerts = True

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.
You need to stop the saving of the file
Code:
[COLOR=#141414][FONT="open sans"]Cancel = True[/FONT][/COLOR]
 
Upvote 0
Thank you, yes I tried that but I could not save the wb with the code in it....



Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = True
End Sub<strike></strike>
 
Upvote 0
You have not actually cancelled the save in your code

Code:
Private Sub Workbook_BeforeClose(Cancel As Boolean)
Me.Saved = True
End Sub

Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = True
End Sub

Before you become frustrated, remember to disable macros BEFORE trying to save the file with the second procedure above
I am sure you can work out why ;)
 
Upvote 0
yes I tried that but I could not save the wb with the code in it....
Chicken and the egg scenario!!

Close the file
To disable macros hold the SHIFT key down before double clicking on the file name when opening the file
Amend the code
Save the file
 
Last edited:
Upvote 0
Did not know about the Shift open. Tried that and it did stop running macros but I still could not save the code. Do I need to Shift while saving too?
 
Upvote 0
This is what I do, and it works for me ...

Hold {SHIFT} as the workbook is opened
Amend the macro
Save as normal
 
Upvote 0
That is what I did but it does not save. Tried holding the shift UNTIL the book was fully open too and still did not work. Any ideas?
 
Upvote 0
So I tried it with only one statement and it worked. This code is the issue, do I need it?

Private Sub Workbook_BeforeClose(Cancel As Boolean)
Me.Saved = True
End Sub
 
Upvote 0
Maybe not - test it

Specifically what happens after you amend something in the workbook and then try to close the workbook (using X in right corner) ?
 
Last edited:
Upvote 0

Forum statistics

Threads
1,223,902
Messages
6,175,278
Members
452,629
Latest member
SahilPolekar

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