Sort dead hyperlinks in Excel with VBA?

前端 未结 2 630
慢半拍i
慢半拍i 2020-12-05 06:08

The title says it:

I have an excel Sheet with an column full of hyperlinks. Now I want that an VBA Script checks which hyperlinks are dead or work and makes an entry

2条回答
  •  慢半拍i
    慢半拍i (楼主)
    2020-12-05 06:54

    Gary's code is perfect, but I would rather use a public function in a module and use it in a cell as function. The advantage is that you can use it in a cell of your choice or anyother more complex function.

    In the code below I have adjusted Gary's code to return a boolean and you can then use this output in an =IF(CHECKHYPERLINK(A1);"OK";"FAILED"). Alternatively you could return an Integer and return the status itself (eg.: =IF(CHECKHYPERLINK(A1)=200;"OK";"FAILED"))

    A1: http://www.whatever.com
    A2: =IF(CHECKHYPERLINK(A1);"OK";"FAILED")

    To use this code please follow Gary's instructions and additionally add a module to the workbook (right click on the VBAProject --> Insert --> Module) and paste the code into the module.

    
    Option Explicit
    
    Public Function CheckHyperlink(ByVal strUrl As String) As Boolean
    
        Dim oHttp As New MSXML2.XMLHTTP30
    
        On Error GoTo ErrorHandler
        oHttp.Open "HEAD", strUrl, False
        oHttp.send
    
        If Not oHttp.Status = 200 Then CheckHyperlink = False Else CheckHyperlink = True
    
        Exit Function
    
    ErrorHandler:
        CheckHyperlink = False
    End Function
    
    

    Please also be aware that, if the page is down, the timeout can be long.

提交回复
热议问题