VBA & Macro Problems

Identify and Delete Unused Excel Names with VBA

Maira MehtabMaira Mehtab Nov 18, 2026 868 views

Question details

Identify Excel defined names that may no longer be needed and delete them without breaking workbook references.

How to Identify and Delete Unused Excel Names with VBA
Product
Microsoft Excel
Device & OS
not provided
Scenario
A workbook contains defined names, and the user wants to use VBA to help find candidates for removal.
Observed behavior
Names must be checked across formulas and other workbook objects before they can be considered unused.
Before you start

Save a separate copy of the workbook before deleting names, and use a macro-enabled copy if you plan to save the VBA code. Check whether the workbook contains charts, validation rules, macros, external links, or other objects that may reference defined names.

Solution 1Recommended

Use VBA to flag names for review, then delete confirmed unused names

Create an inventory with worksheet formula matches to help prioritize names for a broader manual review.

The macro counts formula cells containing each name as text. A zero count does not prove a name is unused: the macro does not check charts, validation rules, conditional formatting, other defined names, VBA, or external workbooks. Text matches can also be false positives and do not resolve name scope.

1
Open the VBA editor

Open the workbook copy in Excel, press Alt+F11, and select Insert > Module in its VBA project.

2
Add the inventory macro

Paste this code into the module: Sub InventoryNames() Dim wb As Workbook, out As Worksheet, ws As Worksheet Dim nm As Name, cellsWithFormulas As Range, c As Range Dim searchName As String, matches As Long, rowNum As Long Set wb = ThisWorkbook Set out = wb.Worksheets.Add out.Range("A:B").NumberFormat = "@" out.Range("A1").Value = "Defined name" out.Range("B1").Value = "Refers to" out.Range("C1").Value = "Formula cells containing name" rowNum = 2 For Each nm In wb.Names searchName = nm.Name If InStrRev(searchName, "!") > 0 Then searchName = Mid$(searchName, InStrRev(searchName, "!") + 1) End If matches = 0 For Each ws In wb.Worksheets If Not ws Is out Then Set cellsWithFormulas = Nothing On Error Resume Next Set cellsWithFormulas = ws.UsedRange.SpecialCells(xlCellTypeFormulas) On Error GoTo 0 If Not cellsWithFormulas Is Nothing Then For Each c In cellsWithFormulas.Cells If InStr(1, CStr(c.Formula), searchName, vbTextCompare) > 0 Then matches = matches + 1 End If Next c End If End If Next ws out.Cells(rowNum, 1).Value = nm.Name out.Cells(rowNum, 2).Value = nm.RefersTo out.Cells(rowNum, 3).Value = matches rowNum = rowNum + 1 Next nm out.Columns("A:C").AutoFit End Sub

3
Run and review the inventory

Place the cursor inside InventoryNames and press F5. Review the new worksheet, starting with names that have zero formula matches; check their scope and references in Formulas > Name Manager.

4
Delete confirmed unused names

Inspect relevant charts, validation rules, conditional formatting, other names, VBA code, and external links. In Formulas > Name Manager, select only names you have confirmed are unused, click Delete, and confirm.

5
Test the workbook

Recalculate the workbook and test affected formulas, charts, validation rules, and macros. If something breaks, reopen the saved copy and reassess the deleted name.

Use VBA to flag names for review, then delete confirmed unused names
Formula matches are only a starting point: This macro does not delete names or verify every possible reference. Use a trusted name-management utility or a more comprehensive audit for complex workbooks.
Free Microsoft Office alternative

Work with Excel spreadsheets in WPS Office

WPS Office is a free, lightweight alternative with a familiar spreadsheet interface and compatibility with Excel formats, making it easy to move everyday spreadsheet work between applications. It does not replace Excel's VBA audit for this task.

Open and edit spreadsheets in a familiar interfaceCompatible with Microsoft Excel formats such as XLSXReview spreadsheet data and formulas in WPS SpreadsheetsA free, lightweight option for everyday office work
microsoft office alternative - wps office

Frequently Asked Questions

Does a zero formula count mean an Excel name is safe to delete?

No. The VBA inventory checks worksheet formula text only. A name may still be used by a chart, validation rule, another defined name, VBA code, or an external workbook.

Where can I see the scope of a defined name?

In Excel, select Formulas > Name Manager and check the Scope column. Names can have workbook or worksheet scope, so similar names may refer to different definitions.

How do I remove a name from the workbook?

After confirming it is unused, open Formulas > Name Manager, select the name, click Delete, and confirm. Test the workbook afterward.

Can I save the inventory macro in an XLSX file?

An XLSX file cannot retain VBA code. To save the macro in the workbook, use File > Save As and choose Excel Macro-Enabled Workbook (.xlsm).