Autolock Cells After Saving With Password to Manually Unlock

WetPaint

New Member
Joined
May 27, 2014
Messages
2
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:
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.
 

Excel Facts

Why does 9 mean SUM in SUBTOTAL?
It is because Sum is the 9th alphabetically in Average, Count, CountA, Max, Min, Product, StDev.S, StDev.P, Sum, VAR.S, VAR.P.
Figured it out: you put your password in both sets of quotations (unprotect password and protect password) instead of just the unprotect line in the above code.
 
Upvote 0

Forum statistics

Threads
1,223,236
Messages
6,170,917
Members
452,366
Latest member
TePunaBloke

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