Pages

Monday, January 30, 2023

How to hide/unhide/delete all named ranges in Excel?

A. Xoa NamesRange

1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.

2. Click Insert > Module, and paste the following code in the Module Window.

Sub ShowNames()
'Update 20140318
Dim xName As Name
For Each xName In Application.ActiveWorkbook.Names
    xName.Visible = True
Next
End Sub

//
Sub DeleteNamesRanged()

Dim counter
Dim nameCount


nameCount = ActiveWorkbook.Names.Count
counter = nameCount
Do While counter > 0
ActiveWorkbook.Names(counter).Delete
counter = counter - 1
Loop
End Sub

//
Sub ShowNamesRanged()

Dim counter
Dim nameCount


nameCount = ActiveWorkbook.Names.Count
counter = nameCount
Do While counter > 0
ActiveWorkbook.Names(counter).Visible
counter = counter - 1
Loop
End Sub


B. Remove Hidden Names

1. Remove 
' Module to remove all hidden names on active workbook
   Sub Remove_Hidden_Names()

       ' Dimension variables.
       Dim xName As Variant
       Dim Result As Variant
       Dim Vis As Variant

       ' Loop once for each name in the workbook.
       For Each xName In ActiveWorkbook.Names

           'If a name is not visible (it is hidden)...
           If xName.Visible = True Then
               Vis = "Visible"
           Else
               Vis = "Hidden"
           End If

           ' ...ask whether or not to delete the name.
           Result = MsgBox(prompt:="Delete " & Vis & " Name " & _
               Chr(10) & xName.Name & "?" & Chr(10) & _
               "Which refers to: " & Chr(10) & xName.RefersTo, _
               Buttons:=vbYesNo)

           ' If the result is true, then delete the name.
           If Result = vbYes Then xName.Delete

           ' Loop to the next name.
       Next xName

   End Sub
2. Sub unhideAllNames()
'Unhide all names in the currently open Excel file
    For Each tempName In ActiveWorkbook.Names
        tempName.Visible = True
    Next
End Sub

No comments: