Find where named ranges are being used in big workbook

后端 未结 3 622
抹茶落季
抹茶落季 2020-12-18 06:22

I have a list of 594 named ranges in a workbook with nearly 20 sheets, each sheet has about 200 columns of data. I need to find out where the named ranges are being used so

3条回答
  •  渐次进展
    2020-12-18 06:40

    This code creates a copy of the workbook with the names. It then goes through and deletes each name in your list of names from the that copied workbook. It counts up the number of formula errors in the workbook before and after. If the error count is the same, the name wasn't used. If it's different, the name was used.

    I like to do this kind of test for really complicated situations like this. It means you don't have to worry so much about complicated rules for testing. You can just base your answer on the results.

    Since the testing is all done on a copy, it should be safe. Be sure to save all your work before though!

    To use, put put your list of names in a workbook and name the range with that list "NamesToTest":

    enter image description here

    Then put this code in the same workbook and run it:

    Sub CheckNameUsage()
    Dim WorkbookWithList As Excel.Workbook
    Dim WorkbookWithNames As Excel.Workbook
    Dim TempWb As Excel.Workbook
    Dim cell As Excel.Range
    Dim NameToCheck As String
    Dim ws As Excel.Worksheet
    Dim ErrorRange As Excel.Range
    Dim ErrorsBefore As Long
    Dim ErrorsAfter As Long
    Dim NameUsed As Boolean
    
    Set WorkbookWithList = ThisWorkbook
    Set WorkbookWithNames = Workbooks("SO - wb to test.xlsx")    'adjust to suit
    WorkbookWithNames.Worksheets.Copy    'Workbooks.Add(WorkbookWithNames.FullName)
    Set TempWb = ActiveWorkbook
    
    For Each cell In WorkbookWithList.Names("NamesToTest").RefersToRange.Cells
        NameToCheck = cell.Value
        ErrorsBefore = 0
        For Each ws In TempWb.Worksheets
            Set ErrorRange = Nothing
            On Error Resume Next
            Set ErrorRange = ws.Cells.SpecialCells(xlCellTypeFormulas, 16)
            On Error GoTo 0
            If Not ErrorRange Is Nothing Then
                ErrorsBefore = ErrorsBefore + ErrorRange.Cells.Count
            End If
        Next ws
        TempWb.Names(NameToCheck).Delete
        ErrorsAfter = 0
        For Each ws In TempWb.Worksheets
            Set ErrorRange = Nothing
            On Error Resume Next
            Set ErrorRange = ws.Cells.SpecialCells(xlCellTypeFormulas, 16)
            On Error GoTo 0
            If Not ErrorRange Is Nothing Then
                ErrorsAfter = ErrorsAfter + ErrorRange.Cells.Count
            End If
        Next ws
        NameUsed = True
        If ErrorsBefore = ErrorsAfter Then
            NameUsed = False
        End If
        Debug.Print NameToCheck; " - Errors Before = " & ErrorsBefore; ", Errors After = " & ErrorsAfter; ", Used = " & NameUsed; ""
    Next cell
    TempWb.Close False
    End Sub
    

    The results will show in the Debug window:

    enter image description here

    The code is hopefully fairly self-explanatory. SpecialCells is worth knowing about, so read up on it if necessary. In this case it identifies cells with errors - that's the 16 argument.

    Note that this only checks for workbook-level names. You could add checks for worksheet-level if necessary.

提交回复
热议问题