We’d like to remind Forumites to please avoid political debate on the Forum.

This is to keep it a safe and useful space for MoneySaving discussions. Threads that are – or become – political in nature may be removed in line with the Forum’s rules. Thank you for your understanding.

📨 Have you signed up to the Forum's new Email Digest yet? Get a selection of trending threads sent straight to your inbox daily, weekly or monthly!
The Forum now has a brand new text editor, adding a bunch of handy features to use when creating posts. Read more in our how-to guide

Deleting Multiple Range names in Excel

I've been searching the net trying to find a macro that will delete all the old range names in my workbook. All the range names start "=#REF!" as they are linking to files that no longer exists. I cam accross the following but it doesn't work. Any help appreciated. Thanks


Sub DeletePhantomRangeNames()

For Each nm In ActiveWorkbook.Names
If nm.RefersTo Like "*REF!*" Then
nm.Delete
End If
Next nm

End Sub


P.S. I've used a macro to do this before but I can't for the life of me find the file :(

Comments

  • ' This procedure deletes every named range in which the RefersTo
    ' argument contains #REF!, where #REF! begins at the second character
    ' and ends at the fifth.

    Sub Delete_Every_Named_Range_With_A_Reference_Error()
    Dim nm As Object
    Dim mystr As String

    ' Loops through each name in the active workbook.
    For Each nm In ActiveWorkbook.Names

    ' Check for #REF! in the defined name reference.
    If InStr(1, nm.RefersTo, "#REF!") Then

    ' Delete the defined name if it is a match.
    nm.Delete

    End If
    Next
    End Sub
    2026 Mortgage-Free Wannabes #24 £8797.50/£20000 OP
  • Gambler
    Gambler Posts: 3,405 Forumite
    Part of the Furniture 1,000 Posts Name Dropper Combo Breaker
    ' This procedure deletes every named range in which the RefersTo
    ' argument contains #REF!, where #REF! begins at the second character
    ' and ends at the fifth.

    Sub Delete_Every_Named_Range_With_A_Reference_Error()
    Dim nm As Object
    Dim mystr As String

    ' Loops through each name in the active workbook.
    For Each nm In ActiveWorkbook.Names

    ' Check for #REF! in the defined name reference.
    If InStr(1, nm.RefersTo, "#REF!") Then

    ' Delete the defined name if it is a match.
    nm.Delete

    End If
    Next
    End Sub

    Thanks. Have tried but getting the same as the earlier macro.

    Run-time error '1004':

    That name is not valid
  • Ummm....

    Do you have any global Workbook name objects with the same name as the local Worksheet name object you are trying to delete?
    2026 Mortgage-Free Wannabes #24 £8797.50/£20000 OP
  • Gambler
    Gambler Posts: 3,405 Forumite
    Part of the Furniture 1,000 Posts Name Dropper Combo Breaker
    Ummm....

    Do you have any global Workbook name objects with the same name as the local Worksheet name object you are trying to delete?

    Ummm...........no idea :o
  • Ok: whats your file saved as and what are the names of your worksheets? ;)
    2026 Mortgage-Free Wannabes #24 £8797.50/£20000 OP
This discussion has been closed.
Meet your Ambassadors

🚀 Getting Started

Hi new member!

Our Getting Started Guide will help you get the most out of the Forum

Categories

  • All Categories
  • 353.8K Banking & Borrowing
  • 254.2K Reduce Debt & Boost Income
  • 455.2K Spending & Discounts
  • 246.9K Work, Benefits & Business
  • 603.4K Mortgages, Homes & Bills
  • 178.2K Life & Family
  • 260.9K Travel & Transport
  • 1.5M Hobbies & Leisure
  • 16K Discuss & Feedback
  • 37.7K Read-Only Boards

Is this how you want to be seen?

We see you are using a default avatar. It takes only a few seconds to pick a picture.