I need this macro todo what it does but exclude the name range "Control1"

tonywatsonhelp

Well-known Member
Joined
Feb 24, 2014
Messages
3,194
Office Version
  1. 365
  2. 2019
  3. 2016
Platform
  1. Windows
Hi Everyone,

I have this macro that removes all name ranges from a document,
it works great

but I would like to keep one called "Control1"

can anyone edit it so "Control1" is not removed

Thanks

Tony

Code:
[COLOR=#000000][FONT=verdana]Option Explicit 
 
Sub RemNamedRanges() 
     
    Dim nm              As Name 
     
    On Error Resume Next 
    For Each nm In ActiveWorkbook.Names 
        nm.Delete 
    Next 
    On Error Goto 0 
     
End Sub [/FONT][/COLOR]
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi Everyone,

I have this macro that removes all name ranges from a document,
it works great

but I would like to keep one called "Control1"

can anyone edit it so "Control1" is not removed

Thanks

Tony

Code:
[COLOR=#000000][FONT=verdana]Option Explicit 
 
Sub RemNamedRanges() 
     
    Dim nm              As Name 
     
    On Error Resume Next 
    For Each nm In ActiveWorkbook.Names 
[/FONT][/COLOR][COLOR=#ff0000][FONT=verdana]if nm.name<>"Control1" then[/FONT][/COLOR][COLOR=#000000][FONT=verdana]
        nm.Delete 
[/FONT][/COLOR][COLOR=#ff0000][FONT=verdana]endif[/FONT][/COLOR][COLOR=#000000][FONT=verdana]
    Next 
    On Error Goto 0 
     
End Sub [/FONT][/COLOR]

How's that?
 
Upvote 0
Maybe:
Code:
Option Explicit
Sub RemNamedRanges()
    Dim nm As Name
    On Error Resume Next
    For Each nm In ActiveWorkbook.Names
        If nm <> "Control1" Then
            nm.Delete
        End If
    Next
    On Error GoTo 0
End Sub
 
Upvote 0
Thank you again Roderrick and thank you Mumps,

Exactly what I needed

Thanks

Tony
 
Upvote 0
Mumps, your code will not work correctly. It is missing something.
This:
Code:
If nm <> "Control1" Then
needs to look like this:
Code:
If nm[COLOR=#ff0000].Name[/COLOR] <> "Control1" Then
like in Roderick's code.

Otherwise it will delete all named ranges!
 
Upvote 0
@Joe4: Thank you. Should have picked up on that.
 
Upvote 0
You are welcome.

Just wanted to make sure that someone doesn't pick up the wrong code and inadvertantly delete something they didn't intend to!
 
Upvote 0

Forum statistics

Threads
1,215,444
Messages
6,124,893
Members
449,194
Latest member
JayEggleton

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