1

I want a MsgBox that displays the names of the "Named Ranges" in the active worksheet, along with the "RefersTo" field.

I created a macro that I thought would work, but when I tried to run it today, it doesn't work.

Does anyone have a solution?

Thanks!

Sub ListNamesActiveSheet()
'
' Based on:
' https://learn.microsoft.com/en-us/office/vba/api/excel.name.refersto
'

Dim ShName As String
Dim strNamesInSheet As String
Dim nm As Name

ShName = ActiveSheet.Name
strNamesInSheet = "Names in Active Sheet:" & vbCr

    For Each nm In ActiveWorkbook.Names
        If InStr(1, ShName, nm.RefersTo) Then
           strNamesInSheet = strNamesInSheet & vbCr & _
                        nm.Name & " " & nm.RefersTo
        End If
    Next
    MsgBox strNamesInSheet

End Sub
2
  • You're searching for named ranges in worksheet names. Just remove the if and end if. Commented Dec 1 at 21:40
  • Excellent! VBasic, thank you so much!! This will make my work much easier. Commented Dec 2 at 22:22

1 Answer 1

0

List Named Ranges of the Active Sheet

Sub ListActiveSheetNamedRanges()
    
    ' Define constants.
    Const PROC_NAME As String = "ListActiveSheetNamedRanges"
    Const REMOVE_SHEET_NAME_FROM_RANGE_NAME As Boolean = False ' worksheet scope
    
    ' Validate the (active) sheet.
    If ActiveSheet Is Nothing Then
        MsgBox "There is no active sheet (""Nothing"")!", _
            vbExclamation, PROC_NAME
        Exit Sub
    End If
    If Not TypeOf ActiveSheet Is Worksheet Then
        MsgBox "The sheet """ & ActiveSheet.Name & """ is no worksheet!", _
            vbExclamation, PROC_NAME
        Exit Sub
    End If
    
    ' Reference (active) sheet, its workbook, and retrieve the sheet's name.
    Dim ws As Worksheet: Set ws = ActiveSheet
    Dim wb As Workbook: Set wb = ws.Parent
    Dim SheetName As String: SheetName = ws.Name
    
    ' Declare additional variables.
    Dim nm As Name, rg As Range
    Dim RangeName As String, ResultString As String
    Dim WasFirstFound As Boolean
    
    ' Loop through the names of the workbook and apply the required logic.
    For Each nm In wb.Names
        Set rg = Nothing ' reset (dereference) on each iteration
        On Error Resume Next ' prevent error when no range
            Set rg = nm.RefersToRange
        On Error GoTo 0
        If Not rg Is Nothing Then ' the name refers to a range
            If rg.Worksheet.Name = SheetName Then ' the range is on the sheet
                ' Write top result rows (only once).
                If Not WasFirstFound Then
                    ResultString = "Named Ranges in Sheet """ & SheetName _
                        & """:" & vbLf
                    WasFirstFound = True ' never reset
                End If
                ' Retrieve the name's name.
                RangeName = nm.Name
                ' Optionally, remove the sheet name from the range name.
                If REMOVE_SHEET_NAME_FROM_RANGE_NAME Then
                    If TypeOf nm.Parent Is Worksheet Then ' is worksheet scope
                        RangeName = Right(RangeName, _
                            Len(RangeName) - InStrRev(RangeName, "!")) ' remove
                    'Else ' is of workbook scope
                    End If
                'Else ' keep sheet name in range name
                End If
                ' Append current named range data to the result string.
                ResultString = ResultString & vbLf _
                    & RangeName & " (""" & nm.RefersTo & """)"
            'Else ' the range is on another sheet
            End If
        'Else ' the name doesn't refer to a range
        End If
    Next nm
    
    ' Display the resulting list in a message box.
    If WasFirstFound Then
        'Debug.Print ResultString
        MsgBox ResultString, vbInformation, PROC_NAME
    Else
        MsgBox "No named ranges found in sheet """ & ws.Name _
            & """ of workbook """ & wb.Name & """!", _
            vbExclamation, PROC_NAME
    End If
    
End Sub
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.