Deleting Named Ranges

Emm

Board Regular
Joined
Nov 29, 2004
Messages
165
Hi All,

I just replied to a previous post, but in hindsight, should have started a new topic.

I have a list of Names that are Named Ranges.
Is it possible to use the list to batch delete the Named ranges instead of looping through each one?

Thanks,

Keith
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
I deleted that post over there (https://www.mrexcel.com/forum/excel...te-named-ranges-reference-errors-ref-vba.html) since you posted it over here.

Assuming that you are wanting to only delete specific named ranges (and not all of them), try setting the names you want to delete in an array. Then you can loop through the array like this:
Code:
Sub DelNamedRanges()

    Dim myNames As Variant
    Dim nr As Integer
    
'   Set array of list of named ranges
    myNames = Array("Dog", "Cat", "Eel")

'   Loop through all named ranges and delete
    For nr = LBound(myNames) To UBound(myNames)
        ThisWorkbook.Names(myNames(nr)).Delete
    Next
    
End Sub
 
Upvote 0
Thanks Joe,

As the names change, can I assign the array to equal the values of cells in a range?

Thanks,

Keith
 
Upvote 0
So, are you saying that the different named ranges you want to delete are listed on your sheet somewhere?

Yes, you can do that. Here is code that looks like the Range Z1:Z3 for the named range list. Note that it will simply ignore any named range it cannot find:
Code:
Sub DelNamedRanges()

    Dim myRange As Range
    Dim cell As Range
    Dim nr As String
    
'   Set range of named range range from cells
    Set myRange = Range("Z1:Z3")

'   Loop through all named ranges and delete
    For Each cell In myRange
        nr = cell.Value
        On Error Resume Next
        ThisWorkbook.Names(nr).Delete
        On Error GoTo 0
    Next cell
    
End Sub
 
Upvote 0

Forum statistics

Threads
1,223,909
Messages
6,175,312
Members
452,634
Latest member
cpostell

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