Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Deleting All Names but a Few.

Deleting All Names but a Few

Written by Allen Wyatt (last updated August 19, 2021)
This tip applies to Excel 97, 2000, 2002, and 2003


6

Do you routinely work with worksheets that contain dozens (or hundreds) of named cells, and most of those names are unnecessary? Cleaning up the names is a huge task, but getting rid of the ones you don't need can make your workbook smaller and more efficient. The problem is, how do you get rid of a lot of unnecessary names all at once? You can certainly delete them one at a time, but such a process quickly gets tiresome.

One possible solution is to simply create a new workbook and copy the cells from the old workbook to the new one. Highlight the cells in the old workbook, use Ctrl+C to copy them, then paste them into worksheets in the new workbook. This will copy almost everything from the old workbook—formulas, formatting, etc. It does not bring copy over print settings or range names. The only task then remaining is to redefine the few names you want in the new workbook.

If you prefer to work with the old workbook (the one with all the names), it is best to create a macro that will do the name deletion for you. You need a macro that will allow you to delete all the names except those you want to keep. The following is a simple approach that accomplishes this task:

Sub DeleteSomeNames()
    Dim vKeep
    Dim nm As Name
    Dim x As Integer
    Dim AWF As WorksheetFunction

    'Add Names to keep here
    vKeep = Array("Name1", "Name2")

    Set AWF = Application.WorksheetFunction
    For Each nm In ActiveWorkbook.Names
        x = 0
        On Error Resume Next
        x = AWF.Match(nm.Name, vKeep, 0)
        On Error GoTo 0
        If x = 0 Then
            nm.Delete
        End If
    Next
    Set AWF = Nothing
End Sub

Before using the macro, modify the line that creates the vKeep array. Simply enter the names you want to keep within the array, each name surrounded by quotes and separated by commas. (In the example shown here, the names "Name1" and "Name2" will be kept.) The macro loops through all the names in the workbook and uses the Match function to see if the name is one in the array. If it is not, then it is deleted.

If you prefer to use a third-party solution to managing the names in your workbook, a great choice is the Name Manager add-in, written by Jan Karel Pieterse. You can find more information on the add-in here:

http://www.jkp-ads.com/officemarketplacenm-en.asp

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (2419) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Deleting All Names but a Few.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Putting a Bullet in the Middle of a Sentence

Need a special character (such as a bullet) in the middle of your text? Here are two quick ways to enter the character ...

Discover More

Cropping Graphics

Excel makes it easy to place a graphic in a worksheet. Once there, you may want to chop off a side (or two) of the ...

Discover More

Typing in Boxes

Do you have bothersome boxes appear around the text in your document? Here's how you can get rid of them and make your ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (menu)

Default Cell Movement when Deleting

Delete a cell or a range of cells, and Excel needs to figure out how to rearrange the void left by the deletion. You can ...

Discover More

Limiting Entry of Prior Dates

Want to establish a "bottom limit" on what dates can be entered in a cell? This tip presents two different ways you can ...

Discover More

Ensuring Rows and Columns are Empty

Before you go about deleting rows and columns helter-skelter, it is a good idea to determine if there is anything in the ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 2 + 2?

2017-02-20 04:13:28

JMJ

@Willy
Thank you for your experiment! I must admit I didn't tested carefully enough all the cases where a name could be used...
I'll try to take them into account in the next version.


2017-02-19 06:23:34

Willy Vanhaelen

@JMJ

THIS IS A VERY DANGEROUS MACRO. I tested it in a workbok with many names and it deleted most of them because they were only used in vba code. It also deleted one used in a conditional format formula.


2017-02-18 09:55:03

Tom Bates

Allen, I'm glad you mentioned the JKP Name Manager - it's an *Excellent* (no pun intended) tool that I've used it for years, and it's free!

Tom


2017-02-18 06:18:27

JMJ

Another solution is to delete only unused names, that's what this macro does:
------------------------------------------------
Public Sub SupprNoms()
'pour se débarrasser des noms inutilisés du classeur.

Dim UnNom As Name
Dim msg As String

On Error Resume Next
msg = ""
For Each UnNom In Names
If Cells.Find(What:=UnNom.Name, _
After:=ActiveCell, _
LookIn:=xlFormulas, _
LookAt:=xlPart, _
SearchOrder:=xlByRows, _
SearchDirection:=xlNext, _
MatchCase:=False, _
SearchFormat:=False).Activate = False Then
msg = msg & UnNom.Name & vbCr
ActiveWorkbook.Names(UnNom.Name).Delete
End If
Next UnNom

On Error GoTo 0

If msg = "" Then
MsgBox "Aucun nom inutilisé dans ce classeur"
Else
MsgBox "Noms supprimés :" & vbCr & msg
End If
End Sub
------------------------------------------------------
Hope this helps !


2016-08-22 05:50:11

jayes

it does nothing on my excel i do not see other names deleted


2012-10-11 10:29:58

Luciana

Good morning.
I tried the macro above, and it works very well to delete all names but a few... However the names that remain on the file have always # REF as reference and before I had no error on the references ... Is there a way to keep the name and the reference to the name ?
Thank you very much
Luciana


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.