How to loop through all named ranges in a sheet?

Ceeyee

Board Regular
Joined
Feb 2, 2011
Messages
164
I want to msgbox each of the names of all the ranges that I have given a name to in a sheet.

How can I do that?

If I do it by checking each cell in the worksheet using .name.name, then when it loop to an un-named cell it gives an object error.

Thanks.
 

Excel Facts

Using Function Arguments with nested formulas
If writing INDEX in Func. Arguments, type MATCH(. Use the mouse to click inside MATCH in the formula bar. Dialog switches to MATCH.
Maybe:
Code:
Sub Ranges()
Dim r As Name

For Each r In ActiveWorkbook.Names
    MsgBox r.Name & " - " & Range(r).Address
Next r

End Sub
 
Upvote 0
This code reports only name that have a range on the first of the active workbook

To look at the second sheet use
Set ws = ActiveWorkbook.Sheets(2)

A sheet called test
Set ws = ActiveWorkbook.Sheets("test")

etc

hth

Dave


Code:
Sub Ranges()
    Dim r As Name
    Dim rng1 As Range
    Dim strOut As String
    Dim ws As Worksheet

    Set ws = ActiveWorkbook.Sheets(1)
    'Not all Names are ranges
    For Each r In ActiveWorkbook.Names
        On Error Resume Next
        If Range(r).Parent.Name = ws.Name Then
            If Err.Number = 0 Then strOut = strOut & r.Name & vbNewLine
        End If
        On Error GoTo 0
    Next r
    If Len(strOut) > 0 Then
        MsgBox "These ranges found in: " & ws.Name & vbNewLine & strOut
    Else
        MsgBox "No names ranges on " & ws.Name
    End If
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,567
Messages
6,179,571
Members
452,927
Latest member
whitfieldcraig

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