Deleting all named ranges in Excel using VBA clears every defined name in the active workbook, which helps clean up clutter, resolve formula errors, or prepare a file for sharing without hidden dependencies. This requires enabling macros and having the Developer tab visible; no other tools or add-ins are needed.
What You'll Need
- Microsoft Excel (2010 or later)
- Developer tab enabled in Excel (to access the VBA editor)
- A workbook with named ranges to delete (optional—you can run the code even if none exist)
Step-by-Step
- Open the Excel workbook where you want to delete all named ranges.
- Press Alt + F11 to open the Visual Basic for Applications (VBA) editor.
- In the VBA editor, go to Insert > Module to create a new blank module.
- Copy and paste the following VBA code into the module window:
Sub DeleteAllNamedRanges() Dim nm As Name On Error Resume Next For Each nm In ThisWorkbook.Names nm.Delete Next nm On Error GoTo 0 MsgBox "All named ranges have been deleted.", vbInformation End Sub - Press Alt + F8 to open the Macro dialog box, select DeleteAllNamedRanges, then click Run.
- Wait for the confirmation message box to appear—this indicates the process completed. Click OK to close it.
- Close the VBA editor and return to Excel. To verify deletion, go to Formulas > Name Manager and confirm the list is empty.
Common Mistakes
- Running the code on the wrong workbook: The code uses
ThisWorkbook, which refers to the workbook containing the macro. If you open another workbook and run the macro from there, it will delete names in *that* workbook instead. Always ensure the macro is saved in the target workbook or explicitly reference the correct workbook object. - Not saving as a macro-enabled file: If you save the workbook as .xlsx after deleting names, the VBA code—and any future use of the macro—will be lost. Save as .xlsm to retain macros.
- Ignoring hidden or system names: Excel sometimes stores internal names (e.g.,
_FilterDatabase,Print_Area) that are essential for features like filters or print areas. Deleting these may break functionality. The provided code deletes *all* names, including hidden ones; use caution if those features matter. - Forgetting to enable macros: If macros are disabled, the code won’t run. Ensure macro security is set to "Enable macros" (File > Options > Trust Center > Trust Center Settings > Macro Settings).
Tips
- Back up first: Always save a copy of your workbook before running this code. Once names are deleted, they cannot be undone with Ctrl+Z.
- Test on a sample file: Try the macro on a non-critical workbook first to observe its behavior.
- Use
Application.DisplayAlerts = Falseto suppress prompts: If Excel asks "Are you sure you want to delete this name?" for each item (rare in modern Excel), temporarily disable alerts by addingApplication.DisplayAlerts = Falsebefore the loop andApplication.DisplayAlerts = Trueafter it. - Filter before deleting: To preserve critical names, modify the loop to skip certain names—for example:
For Each nm In ThisWorkbook.Names If Left(nm.Name, 1) <> "_" Then nm.Delete Next nmThis skips names starting with an underscore (common for system names).
Troubleshooting
- "Run-time error '1004': Application-defined or object-defined error"—This often occurs if Excel tries to delete a name tied to a deleted sheet. Ensure all referenced sheets exist before running the macro. Alternatively, wrap the
nm.Deleteline in an error handler (as shown in the sample code). - Macro doesn’t run or isn’t listed in the dialog: Confirm the code is saved in the correct workbook’s VBA project (not in PERSONAL.XLSB or another template), and check that macros aren’t disabled in Trust Center settings.
- Names still appear after running the macro: Check for workbook-level vs. worksheet-level names. The provided code deletes *workbook-level* names only. To delete *all* names—including those scoped to individual worksheets—use this revised code:
Sub DeleteAllNamedRangesIncludingSheets() Dim nm As Name Dim ws As Worksheet On Error Resume Next ' Delete workbook-level names For Each nm In ThisWorkbook.Names nm.Delete Next nm ' Delete worksheet-level names For Each ws In ThisWorkbook.Worksheets For Each nm In ws.Names nm.Delete Next nm Next ws On Error GoTo 0 MsgBox "All names (workbook and sheet-level) deleted.", vbInformation End Sub - Macro security blocks execution: Go to File > Options > Trust Center > Trust Center Settings > Macro Settings, select "Enable all macros" (temporarily, for testing), or add the workbook location to Trusted Locations.
Frequently Asked Questions
Does deleting named ranges affect formulas that use them?
Yes. If any cells or formulas reference a deleted name, they will show #NAME? errors. Always review dependencies before deleting.Can I recover deleted named ranges?
No—Excel does not support undo for VBA-initiated deletions. Recovery requires restoring from a backup or version history (e.g., OneDrive/Azure file history).Will this delete names in other open workbooks?
No. The code targets onlyThisWorkbook (the file containing the macro). To affect other workbooks, modify the code to loop through Workbooks("filename.xlsx").Names explicitly.
Why do some names reappear after deletion?
Excel may recreate certain names automatically—for example,_FilterDatabase appears when you use AutoFilter on a range. These are system-managed and not editable by users.