2
votes

Unable to delete Named Range with #NAME?

How do I add to my VBA to delete this?

I have created the below code, it deletes all the named ranges in my workbook except for one, details are:

Name: _xlfn.IFERROR Value: #NAME? Refers To: =#NAME?

'Delete All Named Ranges
Dim xName As Name
On Error Resume Next
    For Each xName In ActiveWorkbook.Names
        xName.Visible = True
        If InStr(1, xName.RefersTo, "=#NAME?") > 0 Then xName.Delete
        If InStr(1, xName.RefersTo, "#REF!") > 0 Then xName.Delete
        If InStr(xName.Value, "#REF!") > 0 Then xName.Delete
        xName.Delete
    Next xName
On Error GoTo 0
End Sub```
1

1 Answers

1
votes

The trick is that xName.RefersTo will show the original formula instead of the evaluated value, e.g. =SomeFormulaThatDoesntExist instead of #NAME?

enter image description here

You can overcome it by using Evaluate(xName.RefersTo) which will return Error 2029 for #NAME? error. Furthermore, you can simply add IsError(Evaluate(xName.RefersTo)) condition to your code, something like:

If IsError(Evaluate(xName.RefersTo)) Then xName.Delete