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

Copy formula down without changing references
If you have =SUM(F2:F49) in F50; type Alt+' in F51 to copy =SUM(F2:F49) to F51, leaving the formula in edit mode. Change SUM to COUNT.
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,215,576
Messages
6,125,633
Members
449,242
Latest member
Mari_mariou

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