Delete named ranges from individual worksheet

Domski

Well-known Member
Joined
Jan 18, 2005
Messages
7,292
Hi,

I was after a bit of code to delete the range names on a particular worksheet I thought it couldn't be that hard but have only been able to find this in the archives, unfortunately I get an error when trying to execute it:

Code:
Sub Delete_My_Named_Ranges()
Dim n As Name
Dim Sht As String
' Put in name of sheet where the range is located
Sht = "Org Lookups"
For Each n In ThisWorkbook.Names
If n.RefersToRange.Worksheet.Name = Sht Then
n.Delete
End If
Next n

The error I'm getting is Run-Time error 1004, Application defined or object defined error.

Any suggestions how to fix this?

Dom
 
Ok, weird. I just set up a few ranges in a blank workbook and it works fine. Still won't work in the one I want it to though.
 
Upvote 0

Excel Facts

Which lookup functions find a value equal or greater than the lookup value?
MATCH uses -1 to find larger value (lookup table must be sorted ZA). XLOOKUP uses 1 to find values greater and does not need to be sorted.
How about:
Code:
Sub Delete_My_Named_Ranges()
   Dim n As Name
   Dim Sht As String
   On Error GoTo err_check
   ' Put in name of sheet where the range is located
   Sht = "Org Lookups"
   For Each n In ThisWorkbook.Names
      If n.RefersToRange.Worksheet.Name = Sht Then n.Delete
   Next n
   
Exit Sub
err_check:
   MsgBox "Name: " & n.Name & vbCrLf & "RefersTo: " & n.RefersTo & vbCrLf & "caused an error"
   Resume Next
End Sub
 
Upvote 0
Thanks, that works fine other than the error handler produces the following error:

Name: 'Org Lookups'!_FilterDatabase Refers To: ='Ord Lookups'!#Ref! caused an error.

Part of the code that creates the lists that the named ranges are defined in uses advanced filter to create unique lists and then combines/deletes columns which I'm guessing is creating and then screwing that reference up as it's not one of mine.

Must start using error handlers more in my code I guess.

Dom
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,891
Members
449,194
Latest member
JayEggleton

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