Excel

Delete all named ranges in a workbook containing a #REF! error

Ease of Use

Easy

Version tested with

2003 

Submitted by:

Ken Puls

Description:

This code will delete all named ranges in a workbook which contain a #REF! error 

Discussion:

Code was created to deal with a workbook where a worksheet was deleted after it was no longer necessary. Unfortunately, there were several named ranges referring to this worksheet, and even more that referred to others. This code cycles through all names deleting any with REF! errors, but leaves the rest intact so as not to disrupt the functionality of the workbook. Please Note: If you have a formula on a spreadsheet which refers to a deleted name, it will return a #NAME error. The code makes no provision to fix this type of error. 

Code:

instructions for use

			

Option Explicit Sub DeleteRefErrRanges() 'Macro Purpose: To delete any named range with a "#REF!" error in it Dim nm As Name For Each nm In ActiveWorkbook.Names If InStr(1, nm.RefersTo, "#REF!") > 0 Then nm.Delete Next nm End Sub

How to use:

  1. Copy above code.
  2. In Excel press Alt + F11 to enter the VBE.
  3. Press Ctrl + R to show the Project Explorer.
  4. Right-click desired file on left (in bold).
  5. Choose Insert -> Module.
  6. Paste code into the right pane.
  7. Press Alt + Q to close the VBE.
  8. Save workbook before any other changes.
 

Test the code:

  1. Press Alt + F8 to display the Macro dialog box.
  2. Choose DeleteRefErrRanges and click Okay.
  3. Verify that all named ranges with REF! errors are gone.
 

Sample File:

ClearRefErrNames.zip 8.13KB 

Approved by mdmackillop


This entry has been viewed 153 times.

Please read our Legal Information and Privacy Policy
Copyright @2004 - 2020 VBA Express