Delete all named ranges from worksheet

Martin_H

Board Regular
Joined
Aug 26, 2020
Messages
190
Office Version
  1. 365
Platform
  1. Windows
Hi,

would it be possible to transform this macro to delete named ranges only from active worksheet?

Thank you.

VBA Code:
Sub DeleteNames()
Dim xName As Name
For Each xName In Application.ActiveWorkbook.Names
xName.DELETE
Next
End Sub
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Hi,
try this.
VBA Code:
Sub deleteNames()
    Dim xName As Name
    For Each xName In Application.ActiveWorkbook.Names
        If Split(xName.RefersTo, "!")(0) = "=" & ActiveSheet.Name Then
            xName.Delete
        End If
    Next
End Sub
 
Upvote 0
See Mike Rickson's post #6 here.
according to the method in the link;
VBA Code:
Sub deleteNames()
    Dim xName As Name
    For Each xName In Application.ActiveWorkbook.Names
        If xName.RefersToRange.Parent.Name = ActiveSheet.Name Then
            xName.Delete
        End If
    Next
End Sub
 
Upvote 0
according to the method in the link;
VBA Code:
Sub deleteNames()
    Dim xName As Name
    For Each xName In Application.ActiveWorkbook.Names
        If xName.RefersToRange.Parent.Name = ActiveSheet.Name Then
            xName.Delete
        End If
    Next
End Sub
Not sure why, but I am getting a 1004 error.

Application-defined or object defined error with this line lighlighted:
VBA Code:
If xName.RefersToRange.Parent.Name = ActiveSheet.Name Then
 
Upvote 0
If the name pointing to more than one field is defined as in the picture, it will give an error. In this case, the code in message 2 will check the page name of the first field and will work. If there is a name definition pointing to an area on two separate pages, it will look at the first name in the name definition.
 

Attachments

  • 1629714035028.png
    1629714035028.png
    83.1 KB · Views: 14
Upvote 0
Martin

This should work; NB test on a COPY of your work, until proven. NB - place the code in the "ThisWorkbook" module, in your project.

VBA Code:
Sub deleteNames()
Dim xName As Name
    
With Me
    For Each xName In .Names
        If InStr(xName, "!") > 0 Then
           If xName.RefersToRange.Parent.Name = .ActiveSheet.Name Then
                xName.Delete
           End If
        End If
    Next
End With
End Sub
 
Upvote 0
Solution
Martin

This should work; NB test on a COPY of your work, until proven. NB - place the code in the "ThisWorkbook" module, in your project.

VBA Code:
Sub deleteNames()
Dim xName As Name
   
With Me
    For Each xName In .Names
        If InStr(xName, "!") > 0 Then
           If xName.RefersToRange.Parent.Name = .ActiveSheet.Name Then
                xName.Delete
           End If
        End If
    Next
End With
End Sub

Hi Sykes, I've tried it, but there seems to be a problem with the keyword Me.

Invalid use of Me keyword.
 
Upvote 0

Forum statistics

Threads
1,216,085
Messages
6,128,733
Members
449,465
Latest member
TAKLAM

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