How to delete a single named range from a worksheet

sspatriots

Well-known Member
Joined
Nov 22, 2011
Messages
572
Office Version
  1. 365
Platform
  1. Windows
Hi,

This seems like a silly question, but nothing I've tried seems to work. I have a worksheet called "Jobs" and on that worksheet there is a named range called "HYDROJNRange". I've placed the line of code below in my macro to get rid of it, but I keep getting a Runtime Error 1004 "Application-defined or Object-defined error". Any help on what I am missing here would be greatly appreciated.


Thanks, SS



VBA Code:
    ActiveWorkbook.Names("HYDROJNRange").Delete
 

Excel Facts

Copy a format multiple times
Select a formatted range. Double-click the Format Painter (left side of Home tab). You can paste formatting multiple times. Esc to stop
That works for me, check the spelling of the range.
 
Upvote 0
NamedRange.jpg


It is just a single cell that has that named range. When I select it, I see the named range in the upper left corner that matches what I have in my command line.
 
Upvote 0
I found the following and set the "InStr" name to "HYDROJNRange" and it worked. This is really strange to me, because I think the original line of code I had should have worked.


VBA Code:
Sub DeleteHYDROJNRange()

Dim xName As Name
For Each xName In Application.ActiveWorkbook.Names
    If InStr(xName.Name, "HYDROJNRange") > 0 Then xName.Delete
Next


End Sub
 
Upvote 0
Is it Sheet scoped or Workbook scope?
 
Upvote 0
Solution
If you look in the name manager it will show if a Name is worksheet or workbook scope.
I suspect that yours was worksheet scope & that sheet was not active with your original code.
 
Upvote 0
I looked up what you were asking and it is actually worksheet scoped. I'm guessing it is because the named range is a single cell in a Structured Table in Name Manager. Would that be the case as to why it is worksheet scoped. I see it is greyed out and can't be edited.

I changed that line of code to the following and it worked like you said.

VBA Code:
Sheets("Jobs").Names("HYDROJNRange").Delete
 
Upvote 0
Below is the code that creates that named range. What would I do different if I wanted to make this range a workbook scope named range? Not that I need to know this right now, but always like to learn as much as I can.

VBA Code:
Sub HYDROJNRange()

    Dim iName As String
    iName = "HYDROJNRange"
    ActiveSheet.Names.Add Name:=iName, RefersTo:=Selection

End Sub
 
Upvote 0

Forum statistics

Threads
1,215,095
Messages
6,123,073
Members
449,093
Latest member
ripvw

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