检查Excel工作簿中是否存在损坏的超链接

时间:2017-02-03 00:54:53

标签: excel

通过删除命名范围,指向已命名范围的超链接可能会“损坏”。发生这种情况时,单击超链接将显示消息“参考无效”。有没有办法自动搜索工作簿来查找这些损坏的超链接?

1 个答案:

答案 0 :(得分:0)

在@Ron Rosenfeld建议我需要使用VBA后,我创建了以下内容来解决问题。

Public Sub CheckForBrokenLinks()
    Dim targetWorkbook As Workbook
    Dim searchSheet As Worksheet
    Dim checkLink As Hyperlink
    Dim testRange As Range

    Set targetWorkbook = Application.ActiveWorkbook

    For Each searchSheet In targetWorkbook.Worksheets
        For Each checkLink In searchSheet.Hyperlinks
            If Not checkLink.SubAddress = "" Then
                If Not DoesAddressExist(searchSheet, checkLink.SubAddress) Then
                    Debug.Print "I think the following link is broken:"
                    Debug.Print "   Link name: " & checkLink.Name
                    Debug.Print "   Sheet name: " & searchSheet.Name
                    Debug.Print "   Link target: " & checkLink.SubAddress
                    If checkLink.Type = 0 Then
                        Debug.Print "   Link range: " & checkLink.Range.address
                    End If
                End If
            End If
        Next checkLink
    Next searchSheet
End Sub

Private Function DoesAddressExist(searchSheet As Worksheet, address As String) As Boolean
    Dim testRange As Variant
    On Error Resume Next
    Set testRange = searchSheet.Evaluate(address)
    DoesAddressExist = Err.Number = 0
    Err.Clear
    On Error GoTo 0
End Function