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

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
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,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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