-2

我有一个 Excel 工作簿,在一个工作表上包含数千个命名引用。是否有 VBA 代码可以告诉我在任何其他工作表(超过 25 个)中没有使用这些引用中的哪些(在公式中)。

4

1 回答 1

0

一次编辑。最后我确实使用了很多 getridofnames 谢谢蒂姆。我不得不对其进行大量修改,并且必须循环浏览每个工作表。这很麻烦,但我认为它有效。

Sub RidOfNames() Dim myName As Name Dim i As Integer Dim fdMsg As String Dim t As Boolean

'Search for Sheet1 - If it doesnt exist create it
Dim wsTest As Worksheet
Const strSheetName As String = "Sheet1"

Set wsTest = Nothing
On Error Resume Next
Set wsTest = ActiveWorkbook.Worksheets(strSheetName)
On Error GoTo 0

If wsTest Is Nothing Then
    Worksheets.Add.Name = strSheetName
End If

'Clear all of Sheet1
Sheet1.Cells.Clear

i = 0
On Error Resume Next
fdMsg = ""
For Each myName In Names
    t = False
    For Each ws In ThisWorkbook.Worksheets
        With ws
            'Do not search Raw Data or Sheet 1 - or it has already been found
            If ws.Name = "Sheet1" Or ws.Name = "Raw Data" Or t = True Then GoTo myNext
            ws.Select
            ws.Range("A1").Select
            If Cells.Find(What:=myName.Name, _
               After:=ActiveCell, _
               LookIn:=xlFormulas, _
               LookAt:=xlPart, _
               SearchOrder:=xlByRows, _
               SearchDirection:=xlNext, _
               MatchCase:=False, _
               SearchFormat:=False).Activate Is Nothing Then
                             ' it was not found
                                                        Else
                    t = True ' it was found!
            End If

myNext: 结尾

    Next ws
If t = False Then
    fdMsg = fdMsg & myName.Name & vbCr
    Sheets("Sheet1").Range("A1").Offset(i, 0).Value = myName.Name
    i = i + 1  'increment placement of found named ranges
    t = False  'set test variable back to false
End If
Next myName
If fdMsg = "" Then
    MsgBox "No unused names found in the workbook"
Else
    MsgBox "Names Not Used:" & vbCr & fdMsg
End If

结束子

于 2015-02-16T16:40:44.243 回答