Locking Named Ranges

ScottInTexas

Board Regular
Joined
Oct 28, 2003
Messages
178
I have a zillion named ranges and I need to go through the list and lock them. But I am getting an error "Unable to set the locked property of the Range class." Suppose the range is named LockMe1 then I am trying to do
Code:
Range("LockMe1").Locked=true

Putting this in a loop and cycling through all the Names in the Application should allow me to lock all the ranges I am interested in rather than go through several hundred ranges.

Code:
For each n in Application.Names
      If Range(n.Name).Interior.Color=12343456 then 
          Range(n.Name).Locked=True
      End If
Next

But I get that error.
 

Excel Facts

Easy bullets in Excel
If you have a numeric keypad, press Alt+7 on numeric keypad to type a bullet in Excel.
Hi

Untested but Try

Code:
Sub LockNamedRanges()
  Dim nm As Name
  For Each nm In ThisWorkbook.Names
       With Range(nm)
          .Locked = CBool(.Interior.Color = 12343456)
        End With
  Next nm
End Sub

Dave
 
Upvote 0
You can breakdown the code

VBA Code:
Range("LockMe1").Locked=true

in two parts

VBA Code:
Range("LockMe1").select
Selection.Locked = true
 
Upvote 0
Another option
VBA Code:
Dim n As Name
For Each n In ActiveWorkbook.Names
   If Not n.Name Like "_xl*" Then
      If Range(n.Name).Interior.Color = 12343456 Then
          Range(n.Name).Locked = True
      End If
   End If
Next
 
Upvote 0

Forum statistics

Threads
1,215,420
Messages
6,124,802
Members
449,190
Latest member
cindykay

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