Hi!
I've asked a similar question before, but didn't get very far, so please bear with me.
My problem is the following: I often find myself with excel sheets with a ton (sometimes several hundred) of defined names, most of which are dead. With dead, I mean that they are either literally dead ("#REF") or are linked to other files.
I've written the code below but sometimes get an 1004 - "That name is not valid" error in line "n.delete".
Anyone have any suggestions on how to solve this?
Thanks a lot!
Cheers,
Schwede
I've asked a similar question before, but didn't get very far, so please bear with me.
My problem is the following: I often find myself with excel sheets with a ton (sometimes several hundred) of defined names, most of which are dead. With dead, I mean that they are either literally dead ("#REF") or are linked to other files.
I've written the code below but sometimes get an 1004 - "That name is not valid" error in line "n.delete".
Anyone have any suggestions on how to solve this?
Thanks a lot!
Cheers,
Schwede
Code:
Sub RemoveDeadNames()
Dim n As Name
Dim Total As Integer
Dim WrongRef As Integer
Dim External As Integer
Dim Remaining
Total = 0
WrongRef = 0
External = 0
Remaining = 0
With ActiveWorkbook
For Each n In ActiveWorkbook.Names
Total = Total + 1
If InStr(1, n, "#REF") > 0 Then
n.Delete
WrongRef = WrongRef + 1
ElseIf InStr(1, n, ".xls") > 0 Then
If InStr(1, n, ActiveWorkbook.Name) = 0 Then
n.Delete
External = External + 1
End If
End If
Next n
End With
For Each n In ActiveWorkbook.Names
Remaining = Remaining + 1
Next n
MsgBox "Total number of names: " & Total & Chr(13) & Chr(10) _
& "Wrong references deleted: " & WrongRef & Chr(13) & Chr(10) _
& "External links deleted: " & External & Chr(13) & Chr(10) _
& "Remaining names: " & Remaining
End Sub