How to find and fix broken links between sheets using VBA?

Finding and fixing broken links between Excel sheets involves identifying cells that reference other sheets incorrectly and correcting or updating those references. You can use VBA to help automate parts of this process. Here’s a step by step approach using VBA:

  • Identify Broken Links:

To find broken links, you need to search through the formulas in a workbook to identify any that reference other sheets that don’t exist or are incorrect.

  • Fix Broken Links:

Once you’ve identified broken links, you’ll need a strategy to fix them. This could involve updating the link to a correct sheet name, deleting the link, or prompting the user for input.

Here’s an example of how you might accomplish this using VBA:

Sub FindAndFixBrokenLinks()
    Dim ws As Worksheet
    Dim cell As Range
    Dim formulaCell As Range
    Dim linkTarget As Worksheet
    Dim brokenLink As Boolean
    Dim linkSheetName As String
    Dim response As String
    
    For Each ws In ThisWorkbook.Worksheets
        For Each cell In ws.UsedRange.SpecialCells(xlCellTypeFormulas)
            brokenLink = False
            
            On Error Resume Next
            ' Attempt to evaluate the formula
            If IsError(Evaluate(cell.Formula)) Then
                ' Possible broken link
                brokenLink = True
            End If
            On Error GoTo 0
            
            If brokenLink Then
                ' Extract the sheet name from the formula
                ' Assumes that the references are in the format: =SheetName!A1
                linkSheetName = Split(Mid(cell.Formula, 2), "!")(0)
                
                ' Check if the referenced sheet exists
                On Error Resume Next
                Set linkTarget = ThisWorkbook.Sheets(linkSheetName)
                On Error GoTo 0
                
                If linkTarget Is Nothing Then
                    ' Here, you can decide how to handle the broken link
                    response = InputBox("Broken link detected in " & _
                        ws.Name & ":" & cell.Address & ". Current link: " & _
                        linkSheetName & ". Enter the correct sheet name or leave blank to ignore:", _
                        "Broken Link Detected")
                    
                    If response <> "" Then
                        ' User has entered a new sheet name, attempt to fix link
                        cell.Formula = Replace(cell.Formula, linkSheetName, response)
                    End If
                End If
            End If
        Next cell
    Next ws
    
    MsgBox "Broken link checking complete."
End Sub

Explanation:

  • Iterating Through Sheets and Cells: The code goes through each worksheet and each cell that contains a formula in the `UsedRange` of that worksheet.
  • Checking for Errors: For each formula, it uses `Evaluate` to check if the formula results in an error (which indicates a possible broken link).
  • Extracting and Verifying Links: It extracts the sheet name from the formula and checks if a worksheet with that name exists.
  • User Prompt for Correction: If a broken link is identified (the referenced sheet doesn’t exist), the script prompts the user to input a correct sheet name to attempt to fix the link.
  • Handling Errors: It uses `On Error Resume Next` to bypass runtime errors that occur during the evaluation of formulas or missing worksheets, and resets error handling afterwards.

This script is a starting point, and you can modify the strategy for correcting broken links based on your specific requirements, such as automatically fixing known issues or logging them for manual correction later. As always, make sure you have a backup of your workbook before running scripts that make changes to it.

Unlock Your Potential

Excel

Basic - Advanced

Access

Access Basic - Advanced

Power BI

Power BI Basic - Advanced

Help us grow the project