VBA - Remove all Named Ranges

spydey

Active Member
Joined
Sep 19, 2017
Messages
314
Office Version
  1. 2013
Platform
  1. Windows
I am trying to remove all named ranges/defined names in my workbook.

I was pretty sure that it would be simple.

I am using the following code:

Code:
Sub DeleteNames()


Dim xn As Name


For Each xn In Application.ActiveWorkbook.Names
    xn.Delete
Next
End Sub

However, I get an error message:

Run-time error '1004':

The name that you entered is not valid.

Reasons for this can include:

-The name does not begin with a letter or an underscore
-The name contains a space or other invalid characters
-The name conflicts with an Excel built0in name or the name of another object in the workbook

I have reviewed all the named ranges and none of them appear to fall into those reasons.

A majority of my named ranges use index in the name so that they are dynamic ranges; but I doubt that would be the issue.

Any suggestions or ideas as to why I would be getting this issue? I am racking my brain but can't seem to get it.

-Spydey
 

Excel Facts

Lock one reference in a formula
Need 1 part of a formula to always point to the same range? use $ signs: $V$2:$Z$99 will always point to V2:Z99, even after copying
I avoid using potential column names as variable. Try changing "xn" to "someName"

Thanks Mike for the insight. Sorry it has taken me a bit to get back.

Here is the code that I got to work for what I needed:

Code:
Sub DeleteNames()

Dim RangeName As Name


On Error Resume Next
For Each RangeName In Names
    ActiveWorkbook.Names(RangeName.Name).Delete
Next
On Error GoTo 0
End Sub


Cheers!

-Spydey
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top