VBA: How to delete Name Range based on Name + Scope

losamfr17

Board Regular
Joined
Jun 10, 2016
Messages
149
Hi,

I need a code to delete some duplicate Name Ranges which have identical names but have different scopes. For example, my file has two "List_Tabs" Name Ranges; I want to keep the one that has a Scope = Workbook and delete the one where the Scope refers to tab name "Info".

How do you delete a Name Range based on its name + its scope?

Thank you!
 

Excel Facts

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.
Hi,
Try the below code:
Rich (BB code):
Sub NamesAdd()
  ThisWorkbook.Names.Add "MyName", RefersTo:="Workbook name"
  ThisWorkbook.Sheets(1).Names.Add "MyName", RefersTo:="Sheet name"
End Sub
 
Sub DeleteSheetName()
  Dim n As Name
  For Each n In ThisWorkbook.Names
    If n.Name Like "*!MyName" Then n.Delete
  Next
End Sub
 
Sub DeleteAllSheetNames()
  Dim n As Name
  For Each n In ThisWorkbook.Names
    If Not n.Parent Is ThisWorkbook Then n.Delete
  Next
End Sub
 
Upvote 0
Hi,
Try the below code:
Rich (BB code):
Sub NamesAdd()
  ThisWorkbook.Names.Add "MyName", RefersTo:="Workbook name"
  ThisWorkbook.Sheets(1).Names.Add "MyName", RefersTo:="Sheet name"
End Sub

Sub DeleteSheetName()
  Dim n As Name
  For Each n In ThisWorkbook.Names
    If n.Name Like "*!MyName" Then n.Delete
  Next
End Sub

Sub DeleteAllSheetNames()
  Dim n As Name
  For Each n In ThisWorkbook.Names
    If Not n.Parent Is ThisWorkbook Then n.Delete
  Next
End Sub
How would I write the code if I want to delete all names scoped to a worksheet, except the "Print_Area" names to various worksheets?
I tried this, but it deleted all the Print_Area ones as well.

VBA Code:
Sub DeleteAllSheetNames()
  Dim n As Name
  For Each n In ThisWorkbook.Names
    If Not n.Parent Is ThisWorkbook Then
        If Not n.Name Like "Print_Area" Then n.Delete
    End If
  Next
End Sub
 
Upvote 0
Hi,
Use this code line with asterik symbol: If Not n.Name Like "*Print_Area" Then n.Delete
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,195
Members
452,616
Latest member
intern444

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