vba to delete named ranges returning errors

cjcass

Well-known Member
Joined
Oct 27, 2011
Messages
680
Office Version
  1. 2016
Platform
  1. Windows
Hi,
I have the code below that successfully deletes all named ranges that are displaying #REF!
I tried adapting it to delete all named ranges that are displaying #NAME? by simply changing the #REF! to #NAME? in the code but alas it didn't work. Any thoughts on how this can be achieved?
Any help much appreciated.
VBA Code:
For Each NR In ActiveWorkbook.Names
   If InStr(NR.Value, "#REF!") Then NR.Delete
   Next
 

Excel Facts

Who is Mr Spreadsheet?
Author John Walkenbach was Mr Spreadsheet until his retirement in June 2019.
#REF! and #NAME? are errors
Possibly,


VBA Code:
For Each NR In ActiveWorkbook.Names
   If IsError(Range(NR)) Then NR.Delete
   Next
 
Upvote 0
Maybe with:
VBA Code:
Sub test()
    Dim NR As Object
    For Each NR In ActiveWorkbook.Names
       If Split(NR, "!")(1) = "#REF" Or Split(NR, "!")(1) = "#NAME?" Then NR.Delete
    Next
End Sub
 
Upvote 0
#REF! and #NAME? are errors
Possibly,


VBA Code:
For Each NR In ActiveWorkbook.Names
   If IsError(Range(NR)) Then NR.Delete
   Next
Hi, this one gave me this error...
1682671127419.png
 
Upvote 0
Maybe with:
VBA Code:
Sub test()
    Dim NR As Object
    For Each NR In ActiveWorkbook.Names
       If Split(NR, "!")(1) = "#REF" Or Split(NR, "!")(1) = "#NAME?" Then NR.Delete
    Next
End Sub
Hi, this one gave me this error...
1682671182625.png
 
Upvote 0
What line do you get the error on?

Are you using the whole code including the 'Dim NR As Object'
Hi,
It's on the 3rd line down - If Split(NR, "!")(1) = "#REF" Or Split(NR, "!")(1) = "#NAME?" Then NR.Delete
Yes, using whole code.
Thanks.
 
Upvote 0
Unless you have found a different way of creating the #NAME error, I am not sure you are going to get the intended result.
The only way I can get a #NAME error is to have a named range pointing to a cell that contains a #NAME error.
In this case then deleting the Range Name is not addressing the underlying issue.

If you want to proceed anyway then this might work for you.

VBA Code:
Sub Delete_NameErrors()

    Dim NR As Name

    For Each NR In ActiveWorkbook.Names
        'If IsError(Range(NR.Name)) Then NR.Delete
        If Application.IfError(Evaluate(NR.Value), "DELETE_THIS") = "DELETE_THIS" Then
            NR.Delete
        End If
    Next

End Sub
 
Upvote 0
Unless you have found a different way of creating the #NAME error, I am not sure you are going to get the intended result.
The only way I can get a #NAME error is to have a named range pointing to a cell that contains a #NAME error.
In this case then deleting the Range Name is not addressing the underlying issue.

If you want to proceed anyway then this might work for you.

VBA Code:
Sub Delete_NameErrors()

    Dim NR As Name

    For Each NR In ActiveWorkbook.Names
        'If IsError(Range(NR.Name)) Then NR.Delete
        If Application.IfError(Evaluate(NR.Value), "DELETE_THIS") = "DELETE_THIS" Then
            NR.Delete
        End If
    Next

End Sub
Thanks for your time and response, this one showed a Run-time error '13': Type mismatch on the line If Application.IfError(Evaluate(NR.Value), "DELETE_THIS") = "DELETE_THIS" Then.
As an example, the bottom four in the pic below are what I'm trying to delete:
1682855607999.png
 
Upvote 0
Whoa ! that is a whole different story.
Those range names beginning with _xlfn are normally hidden. Did you unhide them ?
They seem to have something to do with backward compatibility.
Some posts ask about whether the workbook has been opened in in an older version but I just ran an unhide names on a new MS 365 workbook used to look at a question on this Forum that I know used a number of new formulas and it is full of "_xfln" #NAME entries.

Since Excel is creating these hidden entries I suggest leaving them alone.
When I tried to delete them, I got a 1004 error indicating it didn't like trying to access names starting with underscore.
That error might be new because I seem to recall having deleted them in the past to see what would happen and I think Excel just recreated them (but don't quote me on that)
PS: the x & y at the end are variables used in a Let function

1682857991831.png
 
Last edited:
Upvote 0
Solution

Forum statistics

Threads
1,215,737
Messages
6,126,559
Members
449,318
Latest member
Son Raphon

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