how to lock some ranges using VBA

how can i lock multiple ranges in sheet2 using VBA

i am using
ThisWorkbook.Worksheets("Sheet2").Cells.Locked = False
ThisWorkbook.Worksheets("Sheet2").Range("B8:C9,B5:C6,D1:R1,Q4:R4,A12:S17,A19:S33").Locked = True


it is not changing the cell to locked. the locked tick mark does not get ticked when i run the above code. i do not know why
LVL 1
Skylar-BarlowAsked:
Who is Participating?
 
ShumsConnect With a Mentor Distinguished Expert - 2017Commented:
Hi,

First check if your worksheet is protected, if yes then try below code:
ThisWorkbook.Worksheets("Sheet2").Unprotect Password:="Password"
ThisWorkbook.Worksheets("Sheet2").Cells.Locked = False
ThisWorkbook.Worksheets("Sheet2").Range("B8:C9,B5:C6,D1:R1,Q4:R4,A12:S17,A19:S33").Locked = True
ThisWorkbook.Worksheets("Sheets2").Protect Password:="Password", UserInterFaceOnly:=True   'DrawingObjects:=True, Contents:=True, Scenarios:=True

Open in new window

Change the password.
0
 
Skylar-BarlowAuthor Commented:
THANKS.
1
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.