Bulk Url checker macro excel

前端 未结 1 1419
轻奢々
轻奢々 2021-01-16 03:43

Im seeking for help as i have a bulk of links to check if the link is broken i have tried the below macro but it works twice and after that it is no longer working i am usin

相关标签:
1条回答
  • 2021-01-16 04:11

    Edit: I changed your macro to declare variables properly and release objects upon macro completion; this should address any potential memory issues. Please try this code and let me know if it works.

    Sub Audit_WorkSheet_For_Broken_Links()
    
    If MsgBox("Is the Active Sheet a Sheet with Hyperlinks You Would Like to Check?", vbOKCancel) = vbCancel Then
    
        Exit Sub
    
    End If
    
    Dim alink As Hyperlink
    Dim strURL As String
    Dim objhttp As Object
    
    On Error Resume Next
    For Each alink In Cells.Hyperlinks
        strURL = alink.Address
    
        If Left(strURL, 4) <> "http" Then
            strURL = ThisWorkbook.BuiltinDocumentProperties("Hyperlink Base") & strURL
        End If
    
        Application.StatusBar = "Testing Link: " & strURL
        Set objhttp = CreateObject("MSXML2.XMLHTTP")
        objhttp.Open "HEAD", strURL, False
        objhttp.Send
    
        If objhttp.statustext <> "OK" Then
    
            alink.Parent.Interior.Color = 255
        End If
    
    Next alink
    Application.StatusBar = False
    
    'Release objects to prevent memory issues
    Set alink = Nothing
    Set objhttp = Nothing
    On Error GoTo 0
    MsgBox ("Checking Complete!" & vbCrLf & vbCrLf & "Cells With Broken or Suspect Links are Highlighted in RED.")
    
    End Sub
    

    Old Answer Below

    Combining your macro (which seems to be from here) with an alternative found on excelforum yields the below code. Give it a try and let me know if it works for you.

    Sub TestHLinkValidity()
    Dim rRng As Range
    Dim fsoFSO As Object
    Dim strPath As String
    Dim cCell As Range
    
    If MsgBox("Is the Active Sheet a Sheet with Hyperlinks You Would Like to Check?", vbOKCancel) = vbCancel Then
    
        Exit Sub
    
    End If
    
    Set fsoFSO = CreateObject("Scripting.FileSystemObject")
    Set rRng = ActiveSheet.UsedRange.Cells
    For Each cCell In rRng.Cells
        If cCell.Hyperlinks.Count > 0 Then
            strPath = GetHlinkAddr(cCell)
            If fsoFSO.FileExists(strPath) = False Then cCell.Interior.Color = 65535
       End If
    Next cCell
    End Sub
    
    Function GetHlinkAddr(rngHlinkCell As Range)
        GetHlinkAddr = rngHlinkCell.Hyperlinks(1).Address
    End Function
    
    0 讨论(0)
提交回复
热议问题