Delete Named Range in a specific Worksheet in the current Workbook

Knylund

New Member
Joined
Jul 13, 2023
Messages
2
Office Version
  1. 365
Platform
  1. Windows
This is a snippet of code in a larger macro, but this one is giving me a 1004 error and I don't know why:

Public testSheet As String
Public nm As Name
Sub DeleteNamedRangeInWorksheet()

For Each nm In ThisWorkbook.Names
If nm.RefersToRange.Worksheet.Name = testSheet Then
nm.Delete
End If
Next nm
End Sub
My goal is resize the Named Range after adding rows to the range. I have a define Named Range routine that works, so I thought I would delete the named range and then readd it. I did try some resizing code and that gave me even more difficulty, since I have multiple worksheets in the current workbook. I am looking for a reusable sub.
 

Excel Facts

Back into an answer in Excel
Use Data, What-If Analysis, Goal Seek to find the correct input cell value to reach a desired result
This is a snippet of code in a larger macro, but this one is giving me a 1004 error and I don't know why:

Public testSheet As String
Public nm As Name
Sub DeleteNamedRangeInWorksheet()

For Each nm In ThisWorkbook.Names
If nm.RefersToRange.Worksheet.Name = testSheet Then
nm.Delete
End If
Next nm
End Sub
My goal is resize the Named Range after adding rows to the range. I have a define Named Range routine that works, so I thought I would delete the named range and then readd it. I did try some resizing code and that gave me even more difficulty, since I have multiple worksheets in the current workbook. I am looking for a reusable sub.
Have you considered using Excel Tables?

A Table named Range expands as you enter more values in the column or row.
 
Upvote 0
See if the following code works for you:
VBA Code:
Sub AAA()
    For Each nm In ThisWorkbook.Names
        If Replace(Split(nm.Name, "!")(0), "'", "") = testSheet Then nm.Delete
    Next nm
End Sub
 
Upvote 0

Forum statistics

Threads
1,224,586
Messages
6,179,710
Members
452,939
Latest member
WCrawford

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