FREEBIE: Macro to identify, count & list all direct links between sheets in workbook formulae

Col Delane

Active Member
Joined
Jan 14, 2014
Messages
304
In doing some diagnostics on a very large financial model (160Mb) for a client, as part of the "mapping" exercise to get a handle on what the model actually did, I had a need to identify which sheets in the workbook were linked to other sheets, and how many such links there were. Unfortunately, Find only provides a count of the number of CELLS that contain a specified string rather than a count of the number of separate occurrences (e.g. where there are more than one reference to another sheet within the same formulae)

As a result, I developed the following code to loop through all cells containing formulae in each worksheet of the active workbook to ascertain the number of individual occurrences of a each other sheet-name within the formulae on the sheet (i.e. No. of DIRECT intersheet links - so does NOT include links to Defined Names that in turn link to another sheet) and records the results in a matrix, by precedent and dependent sheet, in a new sheet.

I thought others may find app. this useful, so offer it as a pay back for all the help I received from this Forum over the last decade which has helped me get to the level of being able to develop this app.

Any comments, suggestions for improvement, etc. will be greatly appreciated.



Code:
Sub SheetLink_Count()

' Date:     20/01/2014
' Action:   Added
' Author:   Colin Delane, CA & Financial Analyst/Modeller, Perth, Western Australia
' Purpose:  The following code loops through all cells containing formulae in each worksheet of the active workbook to ascertain
'           the number of individual occurrences (NOT cells) of a each other sheet-name within the formulae on the sheet
'           (i.e. No. of [U]DIRECT [/U]intersheet links - so does NOT include links to Defined Names that in turn link to another sheet) and
'           records the results in a matrix, by precedent and dependent sheet, in a new sheet.
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------

    ' Dimension Variables
    Dim wbk As Workbook
    
    Dim wksDep As Worksheet
    Dim wksPrec As Worksheet
    Dim wksRpt As Worksheet
    
    Dim rngSearch As Range
    Dim rngC As Range
    
    Dim intRow As Integer
    Dim intCol As Integer
    
    Dim ipbResp As Variant
    Dim strLink As String
    Dim strMsg As String
        
    Dim lngLinkCount1 As Long
    Dim lngLinkCount2 As Long
    Dim lngLinkCount3 As Long
    
    Dim iTimeStart As Double
    Dim Response1
    
    iTimeStart = VBA.Timer

    On Error Resume Next
    
    Set wbk = ActiveWorkbook
        
    ' Add sheet
    Application.Worksheets.Add
    ActiveSheet.Name = "Internal Link Count"
    Set wksRpt = ActiveSheet
    
    ' Set counters
    intRow = 3  ' First blank row
    intCol = 2  ' First blank column
    
    ' Create table headings:
    ' Dependent Worksheet Names
        For Each wksDep In ActiveWorkbook.Worksheets
            wksRpt.Cells(intRow, 1).Value = wksDep.Name
            intRow = intRow + 1
        Next wksDep
        
        With Columns("A:A")
        .EntireColumn.HorizontalAlignment = xlLeft
        .EntireColumn.AutoFit
        End With
    
    ' Precedent Worksheet Names
        For Each wksPrec In ActiveWorkbook.Worksheets
            With wksRpt.Cells(2, intCol)
            .Value = wksPrec.Name
            .BorderAround (xlContinuous)
            End With
            intCol = intCol + 1
        Next wksPrec
        
        With Range(Cells(2, 2), Cells(2, intCol))
        .Font.Bold = True
        .WrapText = True
        .VerticalAlignment = xlTop
        .EntireColumn.HorizontalAlignment = xlCenter
        .EntireColumn.ColumnWidth = 15
        End With
        
         ' Add headings
        With Range("A1")
            .Value = "As at: " & Format(Date, "dd-mmm-yyyy")
            .Font.Bold = True
            .HorizontalAlignment = xlCenter
        End With
        
        With Range("B1")
            .Value = "Precedent Sheets >>"
            .Font.Bold = True
            .HorizontalAlignment = xlLeft
        End With
        
        With Range("A2")
            .Value = "Dependent Sheet"
            .Font.Bold = True
            .HorizontalAlignment = xlLeft
            .BorderAround (xlContinuous)
        End With
    
    ' Freeze Panes
        Range("B3").Select
          ActiveWindow.FreezePanes = True
      
    ' Enter formulae to sum No. of links in dependent worksheets (by row)
    With Range("A2").Offset(0, intCol - 1)
    .Value = "Total"
    .BorderAround (xlContinuous)
    End With
        
    With Range("A2").Offset(1, intCol - 1)
        .FormulaR1C1 = "=SUM(RC[-" & intCol - 2 & "]:RC[-1])"
        .NumberFormat = "#,##0_);(#,##0); ""- """
        .EntireColumn.HorizontalAlignment = xlCenter
    End With
    Range("A2").Offset(1, intCol - 1).Copy
    Range(Range("A2").Offset(2, intCol - 1), Cells(intRow - 1, intCol)).PasteSpecial xlPasteFormulasAndNumberFormats
    
    ' Build bottom fences & column totals
    With Range("A2").Offset(intRow - 2, 0)
    .FormulaR1C1 = "\-"
    .Copy Range(Range("A2").Offset(intRow - 2, 1), Cells(intRow, intCol))
    End With
    
    With Range("A2").Offset(intRow - 1, 0)
    .Value = "Total"
    .Font.Bold = True
    .HorizontalAlignment = xlLeft
    End With
    
    With Range("A2").Offset(intRow - 1, 1)
    .FormulaR1C1 = "=SUM(R[-" & intRow - 1 & "]C:R[-1]C)"
    .NumberFormat = "#,##0_);(#,##0); ""- """
    .Copy Range(Range("A2").Offset(intRow - 1, 2), Cells(intRow + 1, intCol))
    End With
    
    With Range("A2").Offset(intRow, 0)
    .FormulaR1C1 = "\="
    .Copy Range(Range("A2").Offset(intRow, 1), Cells(intRow + 2, intCol))
    End With
    
    ' Set/Reset counters
    intRow = 3
    intCol = 2
    lngLinkCount1 = 0
    lngLinkCount2 = 0
    lngLinkCount3 = 0
    
    ' Loop thru all worksheets
    For Each wksDep In ActiveWorkbook.Worksheets
    
        On Error GoTo ErrHandler
        
        ' Define range to search
        Set rngSearch = wksDep.UsedRange.SpecialCells(xlCellTypeFormulas)
            
            ' Loop thru each precedent worksheet
            For Each wksPrec In ActiveWorkbook.Worksheets
                strLink = wksPrec.Name
            
                ' Loop thru all cells in the UsedRange of the dependent worksheet (wksDep)
                For Each rngC In rngSearch
                    
                    ' See if strLink occurs in rngC, & if so, count the occurrences
                    lngLinkCount1 = (Len(rngC.Formula) - Len(Replace(LCase(rngC.Formula), LCase(strLink), ""))) / Len(strLink)
                    
                    ' Link occurrence counter
                    If lngLinkCount1 > 0 Then   'Block IF #1
                        
                        ' Add 1 to total
                        lngLinkCount2 = lngLinkCount2 + lngLinkCount1
                        
                    End If  'Block IF #1
                    
                    ' Reset link counter ready for search of next cell
                    lngLinkCount1 = 0
    
                Next rngC
                                         
                 ' Capture total LINK COUNT for each wksDep/wksPrec combination
                 With wksRpt.Cells(intRow, intCol)
                    .Value = lngLinkCount2
                    .NumberFormat = "#,##0_);(#,##0); ""- """
                End With
                
                ' Update/reset counters
                   ' Update grand total counter for links
                    lngLinkCount3 = lngLinkCount3 + lngLinkCount2
                    
                    ' Reset link & column counter ready for next precedent sheet search
                    lngLinkCount2 = 0
                    intCol = intCol + 1
            
            Next wksPrec
            
            'Update / reset counters
            intRow = intRow + 1
            intCol = 2
            lngLinkCount1 = 0
            lngLinkCount2 = 0
            
            Beep
            Beep
            
            Response1 = MsgBox("Continue?", vbYesNo)
            If Response1 = vbNo Then Exit Sub
            
ErrHandler:
    Resume Next
    Next wksDep
    
    If lngLinkCount3 = 0 Then wksRpt.Delete
    
    MsgBox "Search complete." & vbLf & vbLf & lngLinkCount3 & " links found (see Internal Link Count sheet for details)." & vbLf & vbLf & "Time taken: " & VBA.Timer - iTimeStart & " seconds."
    
End Sub
 

Excel Facts

Formula for Yesterday
Name Manager, New Name. Yesterday =TODAY()-1. OK. Then, use =YESTERDAY in any cell. Tomorrow could be =TODAY()+1.
I've amended the code slightly to now include:
  1. temporary switch to manual calculation mode to speed up the process
  2. hyperlinks in place of hard coded values for sheet names (both dependent and precedent sheets) to allow quick navigation

Code:
Sub SheetLink_Count()

' Date:     20/01/2014
' Action:   Added
' Author:   Colin Delane, CA & Financial Analyst/Modeller, Perth, Western Australia
' Purpose:  The following code loops through all cells containing formulae in each worksheet of the active workbook to ascertain
'           the number of individual occurrences (NOT cells) of a each other sheet-name within the formulae on the sheet
'           (i.e. No. of DIRECT intersheet links - so does NOT include links to Defined Names that in turn link to another sheet) and
'           records the results in a matrix, by precedent and dependent sheet, in a new sheet.
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' Dimension Variables
    Dim wbk As Workbook
    
    Dim wksDep As Worksheet
    Dim wksPrec As Worksheet
    Dim wksRpt As Worksheet
    
    Dim rngSearch As Range
    Dim rngC As Range
    
    Dim intRow As Integer
    Dim intCol As Integer
    
    Dim ipbResp As Variant
    Dim strLink As String
    Dim strMsg As String
    
    Dim lngLinkCount1 As Long
    Dim lngLinkCount2 As Long
    Dim lngLinkCount3 As Long
    
    Dim iTimeStart As Double
    Dim Response1
    Dim Calcmode As XlCalculation
    
    iTimeStart = VBA.Timer

    On Error Resume Next
    
    Set wbk = ActiveWorkbook
    
    ' Capture current calc. mode then switch to Manual
    Calcmode = Application.Calculation
    Application.Calculation = xlCalculationManual
    
    ' Add sheet
    Application.Worksheets.Add
    ActiveSheet.Name = "Internal Link Count"
    Set wksRpt = ActiveSheet
    
    ' Set counters
    intRow = 3  ' First blank row
    intCol = 2  ' First blank column
    
    Cells.Interior.ColorIndex = xlColorIndexNone
    
    ' Create table headings:
    ' Dependent Worksheet Names
        For Each wksDep In ActiveWorkbook.Worksheets
            wksRpt.Cells(intRow, 1).Formula = "=HYPERLINK(" & Chr(34) & Chr(35) & Chr(39) & wksDep.Name & "'!$A$1" & Chr(34) & Chr(44) & Chr(34) & wksDep.Name & Chr(34) & ")"
            intRow = intRow + 1
        Next wksDep
        
        With Columns("A:A")
        .EntireColumn.HorizontalAlignment = xlLeft
        .EntireColumn.AutoFit
        End With
    
    ' Precedent Worksheet Names
        For Each wksPrec In ActiveWorkbook.Worksheets
            With wksRpt.Cells(2, intCol)
            .Formula = "=HYPERLINK(" & Chr(34) & Chr(35) & Chr(39) & wksPrec.Name & "'!$A$1" & Chr(34) & Chr(44) & Chr(34) & wksPrec.Name & Chr(34) & ")"
            .BorderAround (xlContinuous)
            End With
            intCol = intCol + 1
        Next wksPrec
        
        With Range(Cells(2, 2), Cells(2, intCol))
        .Font.Bold = True
        .WrapText = True
        .VerticalAlignment = xlTop
        .EntireColumn.HorizontalAlignment = xlCenter
        .EntireColumn.ColumnWidth = 15
        End With
        
         ' Add headings
        With Range("A1")
            .Value = "As at: " & Format(Date, "dd-mmm-yyyy")
            .Font.Bold = True
            .HorizontalAlignment = xlCenter
        End With
        
        With Range("B1")
            .Value = "Precedent Sheets >>"
            .Font.Bold = True
            .HorizontalAlignment = xlLeft
        End With
        
        With Range("A2")
            .Value = "Dependent Sheet"
            .Font.Bold = True
            .HorizontalAlignment = xlLeft
            .BorderAround (xlContinuous)
        End With
    
    ' Freeze Panes
        Range("B3").Select
          ActiveWindow.FreezePanes = True
      
    ' Enter formulae to sum No. of links in dependent worksheets (by row)
    With Range("A2").Offset(0, intCol - 1)
    .Value = "Total"
    .BorderAround (xlContinuous)
    End With
        
    With Range("A2").Offset(1, intCol - 1)
        .FormulaR1C1 = "=SUM(RC[-" & intCol - 2 & "]:RC[-1])"
        .NumberFormat = "#,##0_);(#,##0); ""- """
        .EntireColumn.HorizontalAlignment = xlCenter
    End With
    Range("A2").Offset(1, intCol - 1).Copy
    Range(Range("A2").Offset(2, intCol - 1), Cells(intRow - 1, intCol)).PasteSpecial xlPasteFormulasAndNumberFormats
    
    ' Build bottom fences & column totals
    With Range("A2").Offset(intRow - 2, 0)
    .FormulaR1C1 = "\-"
    .Copy Range(Range("A2").Offset(intRow - 2, 1), Cells(intRow, intCol))
    End With
    
    With Range("A2").Offset(intRow - 1, 0)
    .Value = "Total"
    .Font.Bold = True
    .HorizontalAlignment = xlLeft
    End With
    
    With Range("A2").Offset(intRow - 1, 1)
    .FormulaR1C1 = "=SUM(R[-" & intRow - 1 & "]C:R[-1]C)"
    .NumberFormat = "#,##0_);(#,##0); ""- """
    .Copy Range(Range("A2").Offset(intRow - 1, 2), Cells(intRow + 1, intCol))
    End With
    
    With Range("A2").Offset(intRow, 0)
    .FormulaR1C1 = "\="
    .Copy Range(Range("A2").Offset(intRow, 1), Cells(intRow + 2, intCol))
    End With
    
    ' Set/Reset counters
    intRow = 3
    intCol = 2
    lngLinkCount1 = 0
    lngLinkCount2 = 0
    lngLinkCount3 = 0
    
    ' Loop thru all worksheets
    For Each wksDep In ActiveWorkbook.Worksheets
    
        On Error GoTo ErrHandler
        
        ' Define range to search
        Set rngSearch = wksDep.UsedRange.SpecialCells(xlCellTypeFormulas)
            
            ' Loop thru each precedent worksheet
            For Each wksPrec In ActiveWorkbook.Worksheets
                strLink = wksPrec.Name
            
                ' Loop thru all cells in the UsedRange of the dependent worksheet (wksDep)
                For Each rngC In rngSearch
                    
                    ' See if strLink occurs in rngC, & if so, count the occurrences
                    lngLinkCount1 = (Len(rngC.Formula) - Len(Replace(LCase(rngC.Formula), LCase(strLink), ""))) / Len(strLink)
                    
                    ' Link occurrence counter
                    If lngLinkCount1 > 0 Then   'Block IF #1
                        
                        ' Add 1 to total
                        lngLinkCount2 = lngLinkCount2 + lngLinkCount1
                        
                    End If  'Block IF #1
                    
                    ' Reset link counter ready for search of next cell
                    lngLinkCount1 = 0
    
                Next rngC
       
                ' Capture total CELL COUNT for each wksDep/wksPrec combination
                                    'With wksRpt.Cells(intRow, intCol)
                                    '    .Value = lngClCount1
                                    '    .NumberFormat = "#,##0_);(#,##0); ""- """
                                    'End With
                                    
                 ' Capture total LINK COUNT for each wksDep/wksPrec combination
                 With wksRpt.Cells(intRow, intCol)
                    .Value = lngLinkCount2
                    .NumberFormat = "#,##0_);(#,##0); ""- """
                End With
                
                ' Update/reset counters
                   ' Update grand total counter for links
                    lngLinkCount3 = lngLinkCount3 + lngLinkCount2
                    
                    ' Reset link & column counter ready for next precedent sheet search
                    lngLinkCount2 = 0
                    intCol = intCol + 1
            
            Next wksPrec
            
            'Update / reset counters
            intRow = intRow + 1
            intCol = 2
            lngLinkCount1 = 0
            lngLinkCount2 = 0
            
            Beep
            Beep
                       
ErrHandler:
    Resume Next
    Next wksDep
    
    Range("A2").CurrentRegion.Calculate
    If lngLinkCount3 = 0 Then wksRpt.Delete
    
    ' Reset clac. mode
    Application.Calculation = Calcmode
    
    MsgBox "Search complete." & vbLf & vbLf & lngLinkCount3 & " links found (see Internal Link Count sheet for details)." & vbLf & vbLf & "Time taken: " & VBA.Timer - iTimeStart & " seconds."
    
End Sub
 
Upvote 0
Just passing by to let you know that your code worked PERFECTLY for what I needed. After spending a few hours doing (almost) the exact same thing manually, I noticed it would take me too many hours.
To give you an idea of my case, the Workbook has a total of 96 worksheets and the total count of direct links reached ridiculous 1,5 MILLION links.
Thank you very much!!
 
Upvote 0
I've re-written this so it doesn't include links in the sheet your populating the results to, and also so it clears and reuses the sheet if you re run the code.

Code:
Option Explicit

Private Function SheetExists(SName As String, _
                     Optional ByVal wb As Workbook) As Boolean
'Rowan Ramsay
    On Error Resume Next
    If wb Is Nothing Then Set wb = ActiveWorkbook
    SheetExists = CBool(Len(wb.Sheets(SName).Name))
End Function


Sub SheetLink_Count()


' Date:     20/01/2014
' Action:   Added
' Author:   Colin Delane, CA & Financial Analyst/Modeller, Perth, Western Australia
' Purpose:  The following code loops through all cells containing formulae in each worksheet of the active workbook to ascertain
'           the number of individual occurrences (NOT cells) of a each other sheet-name within the formulae on the sheet
'           (i.e. No. of DIRECT intersheet links - so does NOT include links to Defined Names that in turn link to another sheet) and
'           records the results in a matrix, by precedent and dependent sheet, in a new sheet.
'---------------------------------------------------------------------------------------------------------------------------------------------------------------------------------------
' Dimension Variables
    Dim wbk As Workbook
    
    Dim wksDep As Worksheet
    Dim wksPrec As Worksheet
    Dim wksRpt As Worksheet
    
    Dim rngSearch As Range
    Dim rngC As Range
    
    Dim intRow As Integer
    Dim intCol As Integer
    
    Dim ipbResp As Variant
    Dim strLink As String
    Dim strMsg As String
    
    Dim lngLinkCount1 As Long
    Dim lngLinkCount2 As Long
    Dim lngLinkCount3 As Long
    
    Dim iTimeStart As Double
    Dim Response1
    Dim Calcmode As XlCalculation
    
    iTimeStart = VBA.Timer


    On Error Resume Next
    
    Set wbk = ActiveWorkbook
    
    ' Capture current calc. mode then switch to Manual
    Calcmode = Application.Calculation
    Application.Calculation = xlCalculationManual
    
    ' Add sheet
    If SheetExists("Internal Link Count") = True Then ' Exists
'Set up the wsNamedRanges object to hold the sheet were writing to
        Set wksRpt = Worksheets("Internal Link Count")
'Clear the cells of old data
        wksRpt.UsedRange.Cells.Delete
    Else
'Create a new sheet
        Set wksRpt = Worksheets.Add(After:=Worksheets(Worksheets.Count))
'and Name it
        wksRpt.Name = "Internal Link Count"
    End If


    
    ' Set counters
    intRow = 3  ' First blank row
    intCol = 2  ' First blank column
    
    Cells.Interior.ColorIndex = xlColorIndexNone
    
    ' Create table headings:
    ' Dependent Worksheet Names
        For Each wksDep In ActiveWorkbook.Worksheets
            If wksDep.Name <> wksRpt.Name Then
                wksRpt.Cells(intRow, 1).Formula = "=HYPERLINK(" & Chr(34) & Chr(35) & Chr(39) & wksDep.Name & "'!$A$1" & Chr(34) & Chr(44) & Chr(34) & wksDep.Name & Chr(34) & ")"
                intRow = intRow + 1
            Else: End If
        Next wksDep
        
        With Columns("A:A")
        .EntireColumn.HorizontalAlignment = xlLeft
        .EntireColumn.AutoFit
        End With
    
    ' Precedent Worksheet Names
        For Each wksPrec In ActiveWorkbook.Worksheets
            If wksPrec.Name <> wksRpt.Name Then
                With wksRpt.Cells(2, intCol)
                .Formula = "=HYPERLINK(" & Chr(34) & Chr(35) & Chr(39) & wksPrec.Name & "'!$A$1" & Chr(34) & Chr(44) & Chr(34) & wksPrec.Name & Chr(34) & ")"
                .BorderAround (xlContinuous)
                End With
                intCol = intCol + 1
            Else: End If
        Next wksPrec
        
        With Range(Cells(2, 2), Cells(2, intCol))
        .Font.Bold = True
        .WrapText = True
        .VerticalAlignment = xlTop
        .EntireColumn.HorizontalAlignment = xlCenter
        .EntireColumn.ColumnWidth = 15
        End With
        
         ' Add headings
        With Range("A1")
            .Value = "As at: " & Format(Date, "dd-mmm-yyyy")
            .Font.Bold = True
            .HorizontalAlignment = xlCenter
        End With
        
        With Range("B1")
            .Value = "Precedent Sheets >>"
            .Font.Bold = True
            .HorizontalAlignment = xlLeft
        End With
        
        With Range("A2")
            .Value = "Dependent Sheet"
            .Font.Bold = True
            .HorizontalAlignment = xlLeft
            .BorderAround (xlContinuous)
        End With
    
    ' Freeze Panes
        Range("B3").Select
          ActiveWindow.FreezePanes = True
      
    ' Enter formulae to sum No. of links in dependent worksheets (by row)
    With Range("A2").Offset(0, intCol - 1)
    .Value = "Total"
    .BorderAround (xlContinuous)
    End With
        
    With Range("A2").Offset(1, intCol - 1)
        .FormulaR1C1 = "=SUM(RC[-" & intCol - 2 & "]:RC[-1])"
        .NumberFormat = "#,##0_);(#,##0); ""- """
        .EntireColumn.HorizontalAlignment = xlCenter
    End With
    Range("A2").Offset(1, intCol - 1).Copy
    Range(Range("A2").Offset(2, intCol - 1), Cells(intRow - 1, intCol)).PasteSpecial xlPasteFormulasAndNumberFormats
    
    ' Build bottom fences & column totals
    With Range("A2").Offset(intRow - 2, 0)
    .FormulaR1C1 = "\-"
    .Copy Range(Range("A2").Offset(intRow - 2, 1), Cells(intRow, intCol))
    End With
    
    With Range("A2").Offset(intRow - 1, 0)
    .Value = "Total"
    .Font.Bold = True
    .HorizontalAlignment = xlLeft
    End With
    
    With Range("A2").Offset(intRow - 1, 1)
    .FormulaR1C1 = "=SUM(R[-" & intRow - 1 & "]C:R[-1]C)"
    .NumberFormat = "#,##0_);(#,##0); ""- """
    .Copy Range(Range("A2").Offset(intRow - 1, 2), Cells(intRow + 1, intCol))
    End With
    
    With Range("A2").Offset(intRow, 0)
    .FormulaR1C1 = "\="
    .Copy Range(Range("A2").Offset(intRow, 1), Cells(intRow + 2, intCol))
    End With
    
    ' Set/Reset counters
    intRow = 3
    intCol = 2
    lngLinkCount1 = 0
    lngLinkCount2 = 0
    lngLinkCount3 = 0
    
    ' Loop thru all worksheets
    For Each wksDep In ActiveWorkbook.Worksheets
        If wksDep.Name <> wksRpt.Name Then
        
            On Error GoTo ErrHandler
            
            ' Define range to search
            Set rngSearch = wksDep.UsedRange.SpecialCells(xlCellTypeFormulas)
                
                ' Loop thru each precedent worksheet
                For Each wksPrec In ActiveWorkbook.Worksheets
                    If wksPrec.Name <> "Internal Link Count" Then
                        strLink = wksPrec.Name
                    
                        ' Loop thru all cells in the UsedRange of the dependent worksheet (wksDep)
                        For Each rngC In rngSearch
                            
                            ' See if strLink occurs in rngC, & if so, count the occurrences
                            lngLinkCount1 = (Len(rngC.Formula) - Len(Replace(LCase(rngC.Formula), LCase(strLink), ""))) / Len(strLink)
                            
                            ' Link occurrence counter
                            If lngLinkCount1 > 0 Then   'Block IF #1
                                
                                ' Add 1 to total
                                lngLinkCount2 = lngLinkCount2 + lngLinkCount1
                                
                            End If  'Block IF #1
                            
                            ' Reset link counter ready for search of next cell
                            lngLinkCount1 = 0
            
                        Next rngC
               
                        ' Capture total CELL COUNT for each wksDep/wksPrec combination
                                            'With wksRpt.Cells(intRow, intCol)
                                            '    .Value = lngClCount1
                                            '    .NumberFormat = "#,##0_);(#,##0); ""- """
                                            'End With
                                            
                         ' Capture total LINK COUNT for each wksDep/wksPrec combination
                         With wksRpt.Cells(intRow, intCol)
                            .Value = lngLinkCount2
                            .NumberFormat = "#,##0_);(#,##0); ""- """
                        End With
                        
                        ' Update/reset counters
                           ' Update grand total counter for links
                            lngLinkCount3 = lngLinkCount3 + lngLinkCount2
                            
                            ' Reset link & column counter ready for next precedent sheet search
                            lngLinkCount2 = 0
                            intCol = intCol + 1
                    Else: End If
                Next wksPrec
                
                'Update / reset counters
                intRow = intRow + 1
                intCol = 2
                lngLinkCount1 = 0
                lngLinkCount2 = 0
                
                Beep
                Beep
                           
ErrHandler:
            Resume Next
        Else: End If
    Next wksDep
    
    Range("A2").CurrentRegion.Calculate
    If lngLinkCount3 = 0 Then wksRpt.Delete
    
    ' Reset clac. mode
    Application.Calculation = Calcmode
    
    MsgBox "Search complete." & vbLf & vbLf & lngLinkCount3 & " links found (see Internal Link Count sheet for details)." & vbLf & vbLf & "Time taken: " & VBA.Timer - iTimeStart & " seconds."
    
End Sub
 
Upvote 0
Wow, this is outstanding. Thank you so much for this - EXACTLY what I needed. Like many of us, I inherited a model and finding dependencies was a huge chore. This type of audit - with a matrix output AND hyperlinks - is awesome.
 
Upvote 0
I just found this and would be great if I could use to get through a complex workbook. How exactly should this macro be used? Just place in a module and RUN?

Thanks!
 
Upvote 0
The macro works on the "Active workbook", so you can either:

  1. add it to a standard module in the target workbook, ensure the latter is the active wbk, and run the sub from there, or
  2. add it to a standard module in your "personal macro workbook" (Personal.xlsm), ensure the target wbk is active, and then run the sub.
 
Upvote 0
I've added the macro the active workbook and when I run "SheetLink_Count" I receive the message that the Subscript is out of range and it debugs to "SheetExists = CBool(Len(wb.Sheets(SName).Name))" in the Private Function.

I was looking for a way to post an example attachment but couldn't. I have a huge workbook with many dependents/precedents, that I also receive that same message on, but for simplicity I created a new workbook with only one dependent and still received that message.
 
Upvote 0
1. Ensure that the Private Function SheetExists includes the On Error Resume Next error-handling statement, and that it is active (i.e not "commented out")

2. What version of Excel are you using, and is it Windows or Mac?
  • If it's an early (pre-2007?) or Mac version the CBool type conversion function may not be evaluating correctly if a sheet named "Internal Link Count" does NOT already exist in the active workbook. If possible, you could also consider testing the code on another PC that has a recent Windows version installed.
  • I've also heard that a phantom BreakPoint can sometimes lurk amongst code without any visual indication that it exists, so try pressing F5 when the macro stops to see if that allows it to continue. If this confirms a BP exists, I believe the only way to clear it is to (1) remove the module containing the phantom BP (be sure to preserve the text of the code therein elsewhere first!), (2) save and close the workbook, (3) re-open the workbook, (4) add a module, and (5) reinstate the code.
 
Upvote 0

Forum statistics

Threads
1,214,653
Messages
6,120,751
Members
448,989
Latest member
mariah3

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top