VBA "Activesheet" delete Named Ranges

Shiseiji

Board Regular
Joined
Oct 23, 2009
Messages
214
Office Version
  1. 2019
Platform
  1. Windows
All the code I've found for removing worksheet defined names is similar to the code below and requires the name of the specific worksheet:
VBA Code:
Sub RemoveWsRangeNames()
'7-21-22
    Dim rngName       As Name
    Dim ThisWb      As Workbook
    Dim ThisWs      As Worksheet
'
    Set ThisWb = ActiveWorkbook
    Set ThisWs = ActiveSheet
'
    With ThisWb
    With ThisWs
'---remove any old defined names ---
    For Each rngName In ThisWb.Names
      If InStr(1, rngName, "ws_2Users") Then
    rngName.Delete
    End If
    Next
End With 'ThisWs
End With 'ThisWb
End Sub
I can put this code at the beginning of each module, but a module I can call is of course a much better option.
Any ideas?

TIA

Ron
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
If you put the code in a "General" module, instead of a specific Sheet module, then you should be able to call it from anywhere in your code (in any module) with a line in your code like:
VBA Code:
Call RemoveWsRangeNames
 
Upvote 0
You could also trim the code down a bit, if you look carefuly you can see that 'ThisWs' is not being used.

Also i tend not to use the 'With' statement unless i am writing things more than once:
VBA Code:
Sub RemoveWsRangeNames()
    Dim rngName As Name
   
    For Each rngName In ThisWorkbook.Names
        If InStr(1, rngName, "ws_2Users") Then
            rngName.Delete
        End If
    Next
End Sub
 
Upvote 0
Thanks, I'll give that a try. I'm a hack and tend to stick with what others have shown works. I'll remember that about "With" statements. You can see my notes to ensure I keep up with what they have been used for.

VBA Code:
For Each rngName In ThisWb.Names
      If InStr(1, rngName, "ws_2Users") Then

Ops, that deletes all the workbook names!

Should have been:
VBA Code:
For Each rngName In ThisWs.Names
      If InStr(1, rngName, "ws_2Users") Then

Ron
 
Upvote 0
I am not sure that you can loop through the names on a particular sheet, I have not done it.

As a workaround I have used:
VBA Code:
Sub RemoveWsRangeNames()
    Dim rngName As Name
    Dim ws As String
    
    ws = ActiveSheet.Name
    For Each rngName In ActiveWorkbook.Names
        If rngName.RefersToRange.Worksheet.Name = ws Then
            rngName.Delete
        End If
    Next rngName
End Sub
 
Upvote 0

Forum statistics

Threads
1,216,077
Messages
6,128,680
Members
449,463
Latest member
Jojomen56

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