Unable to delete name range

ryan8200

Active Member
Joined
Aug 21, 2011
Messages
357
I have tried to delete name range in Sheet2 using method 1 and method 2 as below but not workable. One of the name range contains underscore. Error message for both method:
Method 1: Run time error 1004. The syntax of the name is incorrect
Method 2: Run time error 1004. Application-defined or object-defined error

Method 1
VBA Code:
Sub DeleteNamedRanges()

Dim MyName As Name
For Each MyName In Names

    ActiveWorkbook.Names(MyName.Name).Delete

Next

Method 2
VBA 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 = "Sheet2"

For Each n In ActiveWorkbook.Names
If n.RefersToRange.Worksheet.Name = Sht Then
n.Delete
End If
Next n

End Sub
 

Excel Facts

Test for Multiple Conditions in IF?
Use AND(test, test, test, test) or OR(test, test, test, ...) as the logical_test argument of IF.
Hi ryan8200,

maybe try
VBA Code:
Sub DeleteNames()

  Dim nmeName     As Name

  If ActiveWorkbook.Names.Count = 0 Then
    MsgBox "No names found", vbInformation, "Message"
    Exit Sub
  End If

  For Each nmeName In ActiveWorkbook.Names
    nmeName.Delete
  Next nmeName

End Sub
Ciao,
Holger
 
Upvote 0
Hi ryan8200,

maybe try
VBA Code:
Sub DeleteNames()

  Dim nmeName     As Name

  If ActiveWorkbook.Names.Count = 0 Then
    MsgBox "No names found", vbInformation, "Message"
    Exit Sub
  End If

  For Each nmeName In ActiveWorkbook.Names
    nmeName.Delete
  Next nmeName

End Sub
Ciao,
Holger
Hi HaHoBe, thanks for input. Can the name range refer to sheets that consist underscore (Eg: Sales_2022)?
 
Upvote 0
Hi ryan8200,

it worked on my test. But the comparision seems to be case sensitive so I slightly altered the code to look like
VBA Code:
Sub Delete_My_Named_Ranges_mod()
Dim n As Name
Dim strShName As String

' Put in name of sheet where the range is located
strShName = "Test_Underscore"

For Each n In ActiveWorkbook.Names
  If UCase(n.RefersToRange.Worksheet.Name) = UCase(strShName) Then
    n.Delete
  End If
Next n

End Sub
Ciao,
Holger
 
Upvote 0

Forum statistics

Threads
1,223,886
Messages
6,175,191
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