Hidden Named Ranges: Making Visible then Delete

jacobrcotton

Board Regular
Joined
Jan 28, 2017
Messages
51
Hello hello!

My company has a bad habit of recycling spreadsheets instead of going back to source templates. We are running into a problem of having thousands (yes, thousands. don't ask.) of hidden named ranges that reference (1) nothing at all, producing a "#REF!" error, (2) a website link, producing a value of "{...}" or (3) something else entirely resulting in another error.

My goal is one of two (or both?) macros.

(1) If a named range is hidden, then unhide it & delete it.

Code:
Sub DeleteHiddenNames()

    Dim wbName As Name

    For Each wbName In ActiveWorkbook.Names
        If wbName.Visible = False Then
            ActiveWorkbook.Names(wbName.Name).Visible = True
            ActiveWorkbook.Names(wbName.Name).Delete
        End If
    Next wbName
    
End Sub

I am throwing an error at line ActiveWorkbook.Names(wbName.Name).Delete. The error is "run time 1004: the name you entered was invalid".


(2) If a named range has a value that is an error or whatever {...} is, delete it.

Code:
Sub DeleteErrorNames()   
   Call UnhideAllNames

   Dim wbName As Name

    For Each wbName In ActiveWorkbook.Names
        If ISERROR(wbName.Value) or wbName.Value = "{...}" Then
            ActiveWorkbook.Names(wbName.Name).Delete
        End If
    Next wbName
End Sub

This code simply isn't doing anything.

Any guidance would be much appreciated.
 

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.
How about ...

Code:
Sub DeleteHiddenNames()
  Dim oName As Name

  For Each oName In ActiveWorkbook.Names
    If Not oName.Visible Then oName.Delete
  Next oName
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,828
Messages
6,181,210
Members
453,023
Latest member
alabaz

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