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

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
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,223,896
Messages
6,175,263
Members
452,627
Latest member
KitkatToby

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