Change background color of all named ranges

bradyboyy88

Well-known Member
Joined
Feb 25, 2015
Messages
562
HI,

I am trying to loop through all the cells in thisworkbook and change the background color to vbyellow. My code so far is as follows:

Code:
Sub testdata()


Dim nm As Variant


For Each nm In ThisWorkbook.Names
    Range(nm.RefersTo).Interior.Color = vbYellow
Next nm


End Sub

It draws an error at the range() function which makes me think this method will not work. Any help will be appreciated.
 

Excel Facts

Control Word Wrap
Press Alt+Enter to move to a new row in a cell. Lets you control where the words wrap.
First, nm.refersto includes an equal sign which you have to eliminate to create a legal string address. Also, sometimes worksheet function errors cause Excel to generate hidden names you don't know are there.

Not sure, but see if this works for you:
Code:
Sub testdata()
Dim nm As Variant
For Each nm In ThisWorkbook.Names
    If Not nm.Name Like "_xlfn*" Then
        Range(Right(nm.RefersTo, Len(nm.RefersTo) - 1)).Interior.Color = vbYellow
    End If
Next nm
End Sub
 
Upvote 0
First, nm.refersto includes an equal sign which you have to eliminate to create a legal string address. Also, sometimes worksheet function errors cause Excel to generate hidden names you don't know are there.

Not sure, but see if this works for you:
Code:
Sub testdata()
Dim nm As Variant
For Each nm In ThisWorkbook.Names
    If Not nm.Name Like "_xlfn*" Then
        Range(Right(nm.RefersTo, Len(nm.RefersTo) - 1)).Interior.Color = vbYellow
    End If
Next nm
End Sub

Thanks Joe but this code draws a run-time error 1004 Method Range of Object _ Global Fail. Any ideas? Some cells seem to highlight though then hits this error.
 
Upvote 0
How about
Code:
Sub bradyboyy()
    Dim Nme As Name
    For Each Nme In ThisWorkbook.Names
        If Nme.Visible Then
            If InStr(1, Nme, "!") > 0 Then Range(Nme).Interior.Color = vbYellow
        End If
    Next Nme
End Sub
 
Upvote 0
When that error occurs, I assume it highlights the Range(....) line, open the Immediate Window and type:?nm.Name then press enter. If that returns a name, then type: ?nm.RefersTo and press enter. What do you get?
 
Upvote 0
It looks like one of the name ranges show the value as =#REF!=#REF! which is strange. Any ideas why it would show that even though that doesnt seem to be in the named range list. Using the updated code above seems to work but I am wondering which range could cause a reference error. Does checking if its visible miss ones that are in hidden rows ?

Code:
Sub FormattingRange()
    Dim Nme As Name
    For Each Nme In ThisWorkbook.Names
        If Nme.Visible And Nme <> "=#REF!#REF!" Then
            If InStr(1, Nme, "!") > 0 Then Range(Nme).Interior.Color = vbYellow
        End If
    Next Nme
End Sub
 
Upvote 0
It looks like one of the name ranges show the value as =#REF!=#REF! which is strange. Any ideas why it would show that even though that doesnt seem to be in the named range list. Using the updated code above seems to work but I am wondering which range could cause a reference error. Does checking if its visible miss ones that are in hidden rows ?

Code:
Sub FormattingRange()
    Dim Nme As Name
    For Each Nme In ThisWorkbook.Names
        If Nme.Visible And Nme <> "=#REF!#REF!" Then
            If InStr(1, Nme, "!") > 0 Then Range(Nme).Interior.Color = vbYellow
        End If
    Next Nme
End Sub
If you open the Name Manager you can filter for names with errors and delete them. From what you show above it seems the name was on a sheet that's no longer part of the workbook.
 
Upvote 0
Here's my cursory attempt to combine all the knowledge imparted by the previous posts to arrive at something more robust:
Code:
Sub testdata()
Dim nm As Variant
For Each nm In ThisWorkbook.Names
    If nm.Visible And InStr(nm.RefersTo, "!") > 0 And InStr(nm.RefersTo, "#REF!") = 0 Then
        Range(nm).Interior.Color = vbYellow
    End If
Next nm
End Sub
 
Upvote 0
How about
Code:
For Each nm in ThisWorkbook.Names
    On Error Resume Next
        nm.RefersToRange.Interior.Color = vbYellow
    On Error Goto 0
Next nm
 
Upvote 0

Forum statistics

Threads
1,214,646
Messages
6,120,716
Members
448,985
Latest member
chocbudda

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