Issue deleting named ranges

knhelpme

New Member
Joined
Jul 13, 2021
Messages
3
Office Version
  1. 2019
I have many excel files that have bad hidden named ranges (old files I'm trying to clean up for folks). I have tried all of the 'delete range names' suggestions, but I still keep running across a named range called "=#NAME?" - and so the code bombs at this point and nothing is deleted.

the last one I tried was this, where the error was oName= "=#NAME?"
Any help is much appreciated.

Sub DeleteHiddenNames()
Dim oName As Name

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

Excel Facts

Last used cell?
Press Ctrl+End to move to what Excel thinks is the last used cell.
Is it worth trying skipping over the error ?

VBA Code:
Sub DeleteHiddenNames()
   Dim oName As Name

   On Error Resume Next
   For Each oName In ActiveWorkbook.Names
       If Not oName.Visible Then oName.Delete
   Next oName
End Sub
 
Upvote 0
Thanks. This looks like it might skip over hidden ranges, and there are a ton of hidden range names referencing old files, etc that I need to clear. The files are taking forever to load. Folks keep copying tabs with the bad range names. I'm just trying to come up with a way to save all of these files without rebuilding.
 
Upvote 0
There is no reason that should skip over hidden ranges. Have you tried it. If should should only skip those that cause the delete statement to error out.

Run another macro before and after that just counts hidden names.
 
Upvote 0
Imo it has nothing to do with hidden names, rather than ones that cannot be resolved, causing the #NAME? error.
This might be of some help.

VBA Code:
Sub DeleteUnresolvedNames()
    Dim oName As Name
    Dim r     As Range

    For Each oName In ActiveWorkbook.Names
        On Error Resume Next
        Set r = oName.RefersToRange
        On Error GoTo 0
        If r Is Nothing Then
            If IsError(Evaluate(oName.Value)) Then
                oName.Delete
            End If
        End If
        Set r = Nothing
    Next oName
End Sub
 
Upvote 0
Solution
Thanks Alex and GWteB - the combo of your solutions worked GREAT.! I was able to delete the bad range names - and then show all named ranges to go back and delete the #NAME? Really appreciate it!
 
Upvote 0

Forum statistics

Threads
1,223,938
Messages
6,175,528
Members
452,651
Latest member
wordsearch

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