Disable manual saving and only saving with pressing button

slora00

New Member
Joined
Sep 8, 2022
Messages
25
Office Version
  1. 2016
Platform
  1. Windows
Hello people, I hope you're all doing great. I have this challenge that haven't been able to crack yet:

I need to disable or "restrict" users of a file to manually save the file (this includes using buttons pinned to the task bar, going to the "File" tab and clicking "Save" and "Save As" commands, and keyboard shortcuts as Ctrl + S).

The only way I want the users of the file to be able to save it is by pressing a button that will run a macro and format a few cells, and ultimately, save the file.

So I need help with a code that disables manual saving and all its' manifestations, and also, lets me run a code by pressing a button and save the file with THAT button.

Thank you in advance.
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
In the ThisWorkbook module:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If Not SaveButtonClicked Then
        Cancel = True
        MsgBox "You can only save this workbook by clicking the SAVE button on the sheet", vbExclamation
    End If
End Sub

In a standard module:
VBA Code:
Public SaveButtonClicked As Boolean

Public Sub Save_Workbook()
    'Call your macro and format the cells here....

    SaveButtonClicked = True
    ThisWorkbook.Save
    SaveButtonClicked = False
    MsgBox "Workbook saved", vbInformation
End Sub
PS - Assign the Save_Workbook macro to your button.
 
Last edited:
Upvote 0
In the ThisWorkbook module:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    If Not SaveButtonClicked Then
        Cancel = True
        MsgBox "You can only save this workbook by clicking the SAVE button on the sheet", vbExclamation
    End If
End Sub

In a standard module:
VBA Code:
Public SaveButtonClicked As Boolean

Public Sub Save_Workbook()
    'Call your macro and format the cells here....

    SaveButtonClicked = True
    ThisWorkbook.Save
    SaveButtonClicked = False
    MsgBox "Workbook saved", vbInformation
End Sub
I tested the BeforeSave method and found that you could still save by clicking Save in the File menu. Or, the Save Icon in the Application Header.
 
Upvote 0
This works for me...
In the ThisWorkbook module:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    SaveAsUI = sv
    If SaveAsUI Then Cancel = True
End Sub

Private Sub Workbook_Open()
    sv = True
End Sub

In the Sheet module where your Button resides:
VBA Code:
Private Sub CommandButton1_Click()
    Call cls
End Sub

In a standard module:
VBA Code:
Option Explicit
Global sv As Boolean

Sub cls()
    sv = False
    ThisWorkbook.Save
    MsgBox "Your workbook is now saved and ready to be closed"
End Sub
 
Upvote 0
Solution
I tested the BeforeSave method and found that you could still save by clicking Save in the File menu. Or, the Save Icon in the Application Header.
The code works for me; it prevents me from saving the workbook using the following methods:
  • Disc icon on the Quick Access Toolbar
  • File -> Save
  • File -> Save As
  • Ctrl+S
The crucial variable is the SaveButtonClicked flag.
 
Upvote 0
This works for me...
In the ThisWorkbook module:
VBA Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    SaveAsUI = sv
    If SaveAsUI Then Cancel = True
End Sub

Private Sub Workbook_Open()
    sv = True
End Sub

In the Sheet module where your Button resides:
VBA Code:
Private Sub CommandButton1_Click()
    Call cls
End Sub

In a standard module:
VBA Code:
Option Explicit
Global sv As Boolean

Sub cls()
    sv = False
    ThisWorkbook.Save
    MsgBox "Your workbook is now saved and ready to be closed"
End Sub
This worked perfectly man! Thank youuuu
 
Upvote 0
You're welcome, I was happy to help as I am sure @John_w and @Skyybot were as well. Thanks for the feedback!!
 
Upvote 0

Forum statistics

Threads
1,223,892
Messages
6,175,236
Members
452,621
Latest member
Laura_PinksBTHFT

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