VBA Disable Save and Save As until Certain Requirements are Met

avd88

Board Regular
Joined
Jan 18, 2016
Messages
112
Hello,

I am trying to create a macro that will enable and disable the ability to save a file unless cells: B5, B10, B38 and F38 have something in them.
Any ideas?

Thank you!
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
How's this? (Needs to be placed in "This Workbook" Object)


Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)

If Sheets("Sheet2").Range("B5") = "" Or Sheets("Sheet2").Range("B5") = "" Or Sheets("Sheet2").Range("B5") = "" Or Sheets("Sheet2").Range("B5") = "" Then
Cancel = True
MsgBox "Save Cancelled"
Else
Cancel = False
End If


End Sub
 
Last edited:
Upvote 0
I use something like this in one of my projects:

Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
    
If WorksheetFunction.CountA( _
sheet1.Range("B5,B10,B38,F38")) < 4 Then
Cancel = True        
End If
End Sub
 
Upvote 0
Thank you for the reply! I am trying out the first code from mrshl9898, it works but now I am running into the issue where it won't let me save a general blank copy of the document.
 
Upvote 0
Two things you could try, one is to exit the sub if you're the user:

Code:
If Application.UserName = "[COLOR=#ff0000]YOU[/COLOR]" Then
    Exit Sub
    End If

or use a password to bypass the sub:

Code:
Dim pass As String
    pass = InputBox("[COLOR=#ff0000]Enter Admin Password, etc, etc[/COLOR]")
    If pass = "[COLOR=#ff0000]1111[/COLOR]" Then Exit Sub

change the code in red to suit your needs.
 
Upvote 0

Forum statistics

Threads
1,215,521
Messages
6,125,306
Members
449,218
Latest member
Excel Master

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