VBA Code to delete all Named Ranges in a worksheet?

Chris The Rock

Active Member
Joined
Feb 24, 2002
Messages
287
Is there a simple line of code that can remove all named ranges in a worksheet? How about all the named ranges in an entire workbook?
 

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Not quite sure what you mean, if you mean remove the data from the named ranges use this:

<pre>
Dim MyName As Name

For Each MyName In Application.Names
Range(MyName).Delete
Next
</pre>

If you mean to remove the names from the ranges, use this:

<pre>
Dim MyName As Name

For Each MyName In Application.Names
MyName.Delete
Next

</pre>

These will work for the whole workbook, not individual sheets

HTH
 
Upvote 0
I will give this a try, thanks.

What I am specifically referring to is the "Names" that you can define using Insert/Name/Define - each of these refers to a group of cells.

I refresh data daily from an external source, and what's been happening is that during the course of the month, each time I've refreshed, it's created a new Named Range. I simply want to remove these Names from the worksheets.
 
Upvote 0
I tried this, (Example #2) and it does everything I want it to...but it also destroys my print areas. Is there any way to get this to work without blowing away the Print Areas?

Thanks!
 
Upvote 0
Nope, there's no way to preserve your print setup.

However, you can try this code. It creates an array and stores your printarea for each page, deletes the names, then resets all of the print areas. Give it a try because I haven't really tested it:

<pre>
Dim MyPrintArea() As String
Dim i As Integer
Dim MyName As Name

ReDim MyPrintArea(Sheets.Count)

For i = 1 To Sheets.Count
MyPrintArea(i) = Sheets(i).PageSetup.PrintArea
Next


For Each MyName In Application.Names
MyName.Delete
Next

For i = 1 To Sheets.Count
Sheets(i).PageSetup.PrintArea = MyPrintArea(i)
Next

</pre>


HTH
 
Upvote 0
MyPrintArea(i) = Sheets(i).PageSetup.PrintArea

THis particular line of code gives me a Run Time Error 13 'Type Mismatch'.
 
Upvote 0
****, you've probably got Charts inserted there as well. If that's the case, use this code:

<pre>
Dim MyPrintArea() As String
Dim i As Integer
Dim MyName As Name

ReDim MyPrintArea(Worksheets.Count)

For i = 1 To Worksheets.Count
MyPrintArea(i) = Worksheets(i).PageSetup.PrintArea
Next


For Each MyName In Application.Names
MyName.Delete
Next

For i = 1 To Worksheets.Count
Worksheets(i).PageSetup.PrintArea = MyPrintArea(i)
Next
</pre>

HTH
 
Upvote 0

Forum statistics

Threads
1,213,531
Messages
6,114,172
Members
448,554
Latest member
Gleisner2

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