How to lock cell's when the cell is empty

n2rhw

New Member
Joined
Sep 10, 2018
Messages
1
I have an Excel spreadsheet with 90 cells that that are unlocked when I protect the sheet
I'm looking to write a macro that will do the following when I run it:
1. Lock the sheet so only unlocked cells are able to be selected
2. Lock the cells B9:J9, B15:J15, B21:J21, B33:J33 and B39:J39
3. Lock the cells B8:J8, B14:J14, B20:J20, B32:J32 and B38:J38 only if the cell is empty, leaving the cell UN-locked if there is a number in it.

Steps 1 & 2 I can figure out, step 3 is the one I'm looking for help on

Thanks
Charles Wallace
 

Some videos you may like

Excel Facts

How to change case of text in Excel?
Use =UPPER() for upper case, =LOWER() for lower case, and =PROPER() for proper case. PROPER won't capitalize second c in Mccartney

davesexcel

Well-known Member
Joined
Feb 26, 2006
Messages
809
If you are using the recorder. Select the cells, then hit F5 and Special cells...

The select blanks and "OK" or constants then "OK"
 

mumps

Well-known Member
Joined
Apr 11, 2012
Messages
8,906
Try:
Code:
Sub LockCells()
    Application.ScreenUpdating = False
    ActiveSheet.Unprotect
    Dim rng As Range
    Range("B9:J9, B15:J15, B21:J21, B33:J33,B39:J39").Locked = True
    For Each rng In Range("B8:J8, B14:J14, B20:J20, B32:J32, B38:J38")
        If rng = "" Then
            rng.Locked = True
        End If
    Next rng
    With ActiveSheet
        .Protect
        EnableSelection = xlUnlockedCells
    End With
    Application.ScreenUpdating = True
End Sub
 

Watch MrExcel Video

Forum statistics

Threads
1,095,746
Messages
5,446,253
Members
405,392
Latest member
Steveoaktree1977

This Week's Hot Topics

Top