Hi,
I'm creating a document (Excel 2010) where employees open the file and book time off by filling out the columns. I have a code which automatically locks any cells containing data upon saving so an employee is able to enter the days they want off without changing anyone else's entries which is exactly what I want. But is there a way where a supervisor can unprotect the document with a password to edit/approve entries? With the current code, anyone is able to unprotect the sheet but I would like to password protect this if at all possible. I've tried entering passwords on the Unprotected Password line of the code but then it doesn't automatically lock/protect after saving.
This is the code I currently have:
Thanks in advance for your help.
I'm creating a document (Excel 2010) where employees open the file and book time off by filling out the columns. I have a code which automatically locks any cells containing data upon saving so an employee is able to enter the days they want off without changing anyone else's entries which is exactly what I want. But is there a way where a supervisor can unprotect the document with a password to edit/approve entries? With the current code, anyone is able to unprotect the sheet but I would like to password protect this if at all possible. I've tried entering passwords on the Unprotected Password line of the code but then it doesn't automatically lock/protect after saving.
This is the code I currently have:
Code:
Private Sub Workbook_BeforeSave(ByVal SaveAsUI As Boolean, Cancel As Boolean)
On Error Resume Next
'Resume to next line if any error occurs
Dim Cell As Range
With ActiveSheet
'first of all unprotect the entire
'sheet and unlock all cells
.Unprotect Password:=""
.Cells.Locked = False
'Now search for non blank cells
'and lock them and unlock blank cells
For Each Cell In ActiveSheet.UsedRange
If Cell.Value = "" Then
Cell.Locked = False
Else
Cell.Locked = True
End If
Next Cell
.Protect Password:=""
'Protect with blank password, you can change it
End With
Exit Sub
End Sub
Thanks in advance for your help.