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

How to show all formulas in Excel?
Press Ctrl+` to show all formulas. Press it again to toggle back to numbers. The grave accent is often under the tilde on US keyboards.
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,223,247
Messages
6,171,007
Members
452,374
Latest member
keccles

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