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
 

Excel Facts

What does custom number format of ;;; mean?
Three semi-colons will hide the value in the cell. Although most people use white font instead.
If you are using the recorder. Select the cells, then hit F5 and Special cells...

The select blanks and "OK" or constants then "OK"
 
Upvote 0
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
 
Upvote 0

Forum statistics

Threads
1,223,231
Messages
6,170,884
Members
452,364
Latest member
springate

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