Disabling the Save

cstimart

Well-known Member
Joined
Feb 25, 2010
Messages
1,180
Long story short, would like to disable any/all saving functionality from a file only AFTER the macro has been run. Is that possible?
 

Excel Facts

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
I was able to create the functionality by using a hidden code w/in the spreadsheet...however, is it possible to use a function/variable?
 
Upvote 0
This will only work on a per instance basis. In other words if you close and reopen the spreadsheet it will reset.

In a standard module, use a Boolean public variable.

Like so:

Code:
Public MacroRunFlag As Boolean

Sub test()
MsgBox "Hello"
MacroRunFlag = True
End Sub

In the Workbook BeforeSave event:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
Cancel = MacroRunFlag
End Sub
 
Upvote 0
Awesome!

Is it then possible to eliminate the "Do you want to save the changes...." prompt when closing the file?
 
Upvote 0
Change the Workbook BeforeSave event code to:

Code:
Private Sub Workbook_Open()
Cancel = MacroRunFlag
Saved = True
End Sub
 
Upvote 0
You didn't. I did, sorry. It should be the BeforeSave event, not the Open event:

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

From the help file:

Excel Developer Reference
Workbook.Saved Property
True if no changes have been made to the specified workbook since it was last saved. Read/write Boolean.
Syntax

expression.Saved

expression A variable that represents a Workbook object.

Remarks

If a workbook has never been saved, its Path property returns an empty string ("").

You can set this property to True if you want to close a modified workbook without either saving it or being prompted to save it.
 
Upvote 0
I now have....

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
If MacroRunFlag = True Then
    MsgBox "File will not save after running the Macro."
    Cancel = MacroRunFlag
    Saved = True
End If
End Sub

...and I still get the prompt.
 
Upvote 0
Do you still have the MacroRunFlag as a public variable in your module.

Have you set this to TRUE at the end of your macro?
 
Upvote 0
Do you still have the MacroRunFlag as a public variable in your module.

Have you set this to TRUE at the end of your macro?

Yes. It will not allow me to save the file after the macro is run if I manually try to save it. However, if I just close the file, it still prompts me to save the changes. (but does not allow any saving).
 
Upvote 0

Forum statistics

Threads
1,224,599
Messages
6,179,827
Members
452,946
Latest member
JoseDavid

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