VBA code to lock ranges with password across multiple sheets

knowleqe

New Member
Joined
May 30, 2017
Messages
4
Hello!

How would I password protect the same ranges across all the sheets in a workbook? For example I would like to lock down ranges H6:19 and E22:E29, across all existing sheets. I'm brand new to VBA and have been trying to look online most of today on how to do this. So far I came across this but it gives me a run-time error '1004' unable to set the locked property of the range class. Debugging points to "Selection.Locked = False"

Sub Protect_Range_Cells()
Dim range_1 As Range
Set range_1 = Range("h9:E8")
Cells.Select
Selection.Locked = False
range_1.Select
Selection.Locked = True
ActiveSheet.Protect DrawingObjects:=False, Contents:=True, Scenarios:=False

End Sub

Does the tab need to be locked first? Is it possible to lock merged cells in the ranges?

Thanks!
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Is the sheet protected when you run that code?
Try putting
VBA Code:
ActiveSheet.Unprotect
at the start of the routine.
 
Upvote 0
Put it somewhere before the .Locked line.
But if you are starting with the sheet UnProtected then your problem lies elsewhere.
 
Upvote 0

Forum statistics

Threads
1,224,802
Messages
6,181,054
Members
453,014
Latest member
Chris258

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