Deleting named ranges with VBA

davesweep

Well-known Member
Joined
Apr 30, 2007
Messages
510
Hi All,

I am trying to use a little code to delete named ranges from a workbook.

Code:
Sub DeleteAllNames()

Dim nName As Name

For Each nName In ActiveWorkbook.Names
nName.Delete
Next nName

End Sub

However, on running the code, I get the dreaded runtime error 1004 telling me the name I have entered is invalid. None of the named ranges are deleted. By putting a break in at line 4, print nName.name returns _xlfn.IFERROR and hovering over the nName variable shows "=#NAME?", yet none of the named ranges contain an IFERROR function.

I fear this might be a result of Mondayitis, but I am truly stumped.
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.
Is this any better?

Code:
Sub DeleteAllNames()
Dim i As Long
For i = ActiveWorkbook.Names.Count To 1 Step -1
    Range(ActiveWorkbook.Names(i).Name).Name.Delete
Next i
End Sub
 
Upvote 0
Hi Peter,

I still get the error with your code.

I have worked around by using On Error Resume Next, but I would rather understand the issue than hide it.
 
Upvote 0

Forum statistics

Threads
1,214,590
Messages
6,120,423
Members
448,961
Latest member
nzskater

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