The Gary code is perfect, but I would rather use a public function in a module and use it as a function. The advantage is that you can use it in a cell of your choice or in any other more complex function.
In the code below, I adjusted the Gary code to return a boolean, and you can use this output in the format = IF (CHECKHYPERLINK (A1), "OK", "FAILED"). Alternatively, you can return Integer and return the status itself (for example: = IF (CHECKHYPERLINK (A1) = 200; "OK", "FAILED"))
A1: http://www.whatever.com
A2: = IF (CHECKHYPERLINK (A1), "OK", "FAILED")
To use this code, follow the Gary instructions and optionally add the module to the book (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
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
Also remember that if the page does not work, the wait time may be long.
source share