Professional Documents
Culture Documents
Content 159 PDF
Content 159 PDF
Comment: This article applies to the 2003 and earlier versions of Excel - it has not been
checked against the latest version. This material is provided for educational purposes - use
it at your own risk.
Names can be used in spreadsheets instead of the A1 style references. The results are easier to read, but there are
some catches
Simple excel spreadsheet with 3 named ranges: a, b and d (the variable c can not be used it defines a column)
Option Explicit
Sub ListNames()
Dim nms As Variant
For Each nms In ActiveWorkbook.Names
Debug.Print nms.Name;
Debug.Print nms.RefersTo
Next nms
End Sub
Disclaimer: This document is provided for educational purposes only. Pressure Vessel Engineering
Ltd. is not liable for its use.
The command Insert / names / paste produces a similar list. Note: sometimes the list created this way is missing
variable names found on other sheets in the same workbook.
If we add another sheet and access the values of a, b and d, the values from sheet 1 will be used the first sheet
to own the names.
When an attempt is made to add a named range a to sheet4, excel refers back to sheet 1 instead. To add a range a
to sheet 4, the sheet needs to be moved into its own workbook, the name added, and then the sheet gets moved back.
If there are links created during this process, then this cannot be done.
Links exist back to the original spreadsheet, the variables a, b and d cannot be added to Sheet 4.
Sheet 5 has been moved back to the original workbook. The names have been successfully re-used.
Successful re-use of names a, b and d by exporting the sheet before adding the names to the list.
An attempt has been made to access a on sheet 1. There already is a range name a on the current sheet, so that
value is used instead.
Workaround:
Note: If this method is used to access a variable name that is not duplicated on the current sheet, then an error will
sometimes result the cell needs to be accessed by clicking #2 above without clicking #1 first.
The names collection keeps the variable name, but sets the location to #ref
Insert / name / paste list with Sheet 2 active now the unused variable name shows.
The paste list command is not very useful for removing unused variable names!
This is a typical routine to remove dead names in a workbook. It looks through the names collection to find #ref
refers to, however, the .delete method does not work. It removes the first copy of the range name, not necessarily
the range name with the #ref. When there only is one copy of each variable name then it works. This is typical of
name collection routines found on the internet. Do not use!
Sub DeleteDeadNamesDoesNotWork()
Dim n As Name
Dim strRefersTo As String
' Loop through the Names collection in this workbook
' Remove dead names
For Each n In ActiveWorkbook.Names
With n
strRefersTo = UCase(.RefersTo)
If InStr(1, strRefersTo, "#REF") Then
' Dead cell reference found - offer user a
chance to delete
If MsgBox("Delete " & .Name & " ? " & vbCrLf &
"Refers to " & .RefersTo, vbYesNo + vbQuestion) = vbYes Then
.Delete
End If
End If
End With
Next
End Sub
Sub DeleteNames3DoesNotWork()
Dim n As Name
Dim strNm As String
Dim strRefTo As String
Dim Pointer As Long
This routine removes the names by their index number. It deletes fewer wrong entries, but still is not reliable for
removal of duplicated names. Even when referred to by their index number, the wrong entry sometimes gets
removed. This removes wrong names about 1/10 as often as the previous routine...
...missing code...
Counter = 0
For Pointer = ActiveWorkbook.Names.Count To 1 Step -1
strNm = ActiveWorkbook.Names(Pointer).Name
strRefTo = ActiveWorkbook.Names(Pointer).RefersTo
If InStr(1, strRefTo, "#REF") Then
ActiveWorkbook.Names(Pointer).Delete
Counter = Counter + 1
End If
Next Pointer
Debug.Print "Removed bad entries = "; Counter
End Sub
Sub DeleteNames6DoesNotWork()
Dim n As Name
Dim strNm As String
Dim strRefTo As String
Dim Pointer As Long
Dim Counter As Long
Dim aBadNames() As Long
End Sub
Version 6 above. First time it is run it removes 119 names. The second time 6 names. All six of which have
duplicate use, and the wrong version of which was originally removed. If a different sheet is active during the name
removal, no wrong names are removed. The correct removal of bad names depends on which sheet is active in
the workbook. Wrong names will be removed from the active sheet only.
Workaround: add a blank sheet to the workbook being cleaned. Run the macro. Remove the blank sheet.
Number of wrong names removed = 0. The macro below does this. It also indicates if any good variable names
were removed. Save your file before running this macro! This is the version of the macro that I currently use.
Sub DeleteNames10Works()
Dim strBlankSheetName As String
Dim NumPass1 As Long
Dim NumPass2 As Long
Dim NumPass3 As Long
Call DeleteNames9(NumPass1)
Call DeleteNames9(NumPass2)
If NumPass2 > 0 Then
Call DeleteNames9(NumPass3)
MsgBox "WARNING - the program has deleted " & NumPass2 +
NumPass3 & " valid names"
Else
MsgBox "The program has sucessfully deleted " & NumPass1
& " bad names"
End If
End Sub
Similarly, this is the routine I use to remove dead links from sheets:
Sub DeleteLinks12()
Dim strBlankSheetName As String
Dim NumPass1 As Long
Call DeleteNames11(NumPass1)
Call DeleteNames11(NumPass2)
If NumPass2 > 0 Then
Call DeleteNames11(NumPass3)
MsgBox "WARNING - the program has deleted " & NumPass2 +
NumPass3 & " valid names"
Else
MsgBox "The program has sucessfully deleted " & NumPass1
& " bad names"
End If
End Sub
Remember: always save your work before running a macro like these!
Laurence Brundrett
Pressure Vessel Engineering Ltd.
Sept 8 2008