Excel VBA - Website Status Checker

jfoBoston

New Member
Joined
Feb 8, 2018
Messages
4
I'm trying to setup an workbook that will check the current status of a website (URL RETURN CODE 200) using a Public Function. While I found an example of one that seems to work like a charm, I'm not sure how it is being triggered and how to "refresh" it.
VBA Code:
Public Function CheckURL(url As String)
    Dim request As Object
    Set request = CreateObject("WinHttp.WinHttpRequest.5.1")
    On Error GoTo haveError
    With request
        .Open "HEAD", url
        .Send
        CheckURL = .Status
        End With
        Exit Function
haveError:
    CheckURL = Err.Description
End Function

Does this automatically check so often or does it simply trigger on open/data refresh?

Thanks in advance for any insight.
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
Are you calling the function from a cell?
Are you desiring to call your function at regular intervals? Or in response to some event?

This tweak will give you a bit better performance and automatically bind to the most recent version. Note Static and dropping the version.

VBA Code:
Public Function CheckURL(url As String)
    Static request As Object
    If request Is Nothing Then Set request = CreateObject("WinHttp.WinHttpRequest")
    On Error GoTo haveError
    With request
        .Open "HEAD", url
        .Send
        CheckURL = .Status
        End With
        Exit Function
haveError:
    CheckURL = Err.Description
End Function

You might gain a negligible performance improvement if you early bind as well by setting a reference to Microsoft WINHTTP Service, Version x.x and changing a couple lines of code.
VBA Code:
    Static request As Object
    If request Is Nothing Then Set request = CreateObject("WinHttp.WinHttpRequest")

to

    Static request As WinHttp.WinHttpRequest
    If request Is Nothing Then Set request = New WinHttp.WinHttpRequest

I would not bother with the optimizations unless you will be calling your function often.
 
Upvote 0

Forum statistics

Threads
1,214,908
Messages
6,122,187
Members
449,072
Latest member
DW Draft

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top