VBA to prevent saving in worksheet if certain cells are empty but not for administrator / creator

Anandpersad

New Member
Joined
Oct 18, 2014
Messages
31
Hi All,

I have the following VBA, which prevent the user from saving the file without filling in the mandatory fields.
The problem is, even I as creator of the VBA cannot save the file without entering data in those cells.
Can you help me with a VBA to avoid this.
So, the user of the file should always fill in the mandatory field before he/she can save the file, but the creator of the file can save without filling in the mandatory file.
I was thinking of a password protection. Only those who has the password can save without filling in he mandatory fields.
But I am also open for any other solution.
My VBA is as follows:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
'Disables saving unless user fills in required cells
Dim CheckC As Range
Set CheckC = Range("H9")
If CheckC.Value = "" Then
MsgBox "Cell H9 Check", vbCritical, "Incomplete Entry"
CheckC.Select
Cancel = True
End If
Set CheckC = Range("H10")
If CheckC.Value = "" Then
MsgBox "Cell H10 check", vbCritical, "Incomplete Entry"
CheckC.Select
Cancel = True
End If
End Sub

<tbody>
</tbody>

Thanks
Anandpersad
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.

JoeMo

MrExcel MVP
Joined
May 26, 2009
Messages
17,585
Office Version
  1. 365
  2. 2010
Platform
  1. Windows
You could add this line immediately after your Dim statement:
Code:
If Environ("UserName") = "Put your user name between these quote marks" Then Exit Sub
 

Anandpersad

New Member
Joined
Oct 18, 2014
Messages
31
HI Joe,

Sorry for the late reply.
It is working perfectly.
Thank you very much.

Anandpersad
 

Forum statistics

Threads
1,144,376
Messages
5,724,001
Members
422,530
Latest member
Badpoisondwarf

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
Top