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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.
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,223,894
Messages
6,175,250
Members
452,623
Latest member
Techenthusiast

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