Delete Named Range if no longer valid

nigelk

Well-known Member
Joined
Aug 30, 2008
Messages
537
Hi all,

I have this code

Code:
  On Error Resume Next
    For Each Nm In ThisWorkbook.Names
        Nm.Delete
    Next Nm
    On Error GoTo 0

Is it possible to change it so that it only removes the names that evaluate to a Ref# error?

Thanks,Nigel
 

Excel Facts

Spell Check in Excel
Press F7 to start spell check in Excel. Be careful, by default, Excel does not check Capitalized Werds (whoops)
Hi Nigel

Try this:

Code:
  On Error Resume Next
    For Each Nm In ThisWorkbook.Names
        If Instr(1,Nm.RefersTo,"#REF")>0 Then Nm.Delete
    Next Nm
    On Error GoTo 0
 
Upvote 0
Tried it but it didn't work.Ran the code and it didn't delete them. Thanks much for the Input.
 
Upvote 0
Richard's code should do the trick for that. I use it all the time to clean out redundant names.

So...
What did you do?
What happened?
What didn't happen?

Denis
 
Upvote 0
Hi Sydney. I typed the code as stated and ran it from VB editor via the run command. The code ran but the defined names with the ref errors are still there in the name manager. I can't delete them thru that because the edit and delete buttons are not enabled.
 
Upvote 0
There are some sheets in the worksheet that are and some that are not. Would that make a difference? The ranges are not valid anymore since I deleted the sheets they were assigned to a while ago (like a Rookie,eh).
 
Upvote 0
It might make a difference. Check to see which sheets those invalid names refer to.
If the protected ones are the problem you will need to unprotect, clean the names, then reprotect.

Denis
 
Upvote 0

Forum statistics

Threads
1,224,584
Messages
6,179,687
Members
452,938
Latest member
babeneker

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