VBA Code to Validate URL and Highlight Cells

  • Hey Guys,


    I have a column full of URL's(from cell B2 down). I just need a macro or something to validate each URL and highlight the cell with different color with respective of the results.


    If the URL is Valid, I want the cell color to be green.


    If the URL redirects, I want the cell color to be yellow.


    Finally, If the URL throws a 404 or any other error page, I want the cell color to be grey.


    few example URL's are:


    Valid One : [TABLE="width: 769"]

    [tr]


    [TD="class: xl64, width: 769"]https://www.wireless.att.com/b…0-bill-credit-details.jsp ( needs to be green)


    Redirected One: [TABLE="width: 769"]

    [tr]


    [TD="class: xl65, width: 769"]https://www.wireless.att.com/b…ns/200-port-in-credit.jsp (needs to be yellow)


    Error Page:[TABLE="width: 769"]

    [tr]


    [TD="class: xl65, width: 769"]https://www.wireless.att.com/b…/100-bill-credit-July.jsp (needs to be grey)[/TD]

    [/tr]


    [/TABLE]
    [/TD]

    [/tr]


    [/TABLE]
    [/TD]

    [/tr]


    [/TABLE]

    Please help, Thanks in Advance.



  • Re: VBA Code to Validate URL and Highlight Cells


    Hi CodeNerd, welcome to Ozgrid :)


    As a starting point, you could just create a UDF that returns the status code and use that with some conditional formatting in your sheet?



    Assuming your URLs are in column A, you would use it in your worksheet like so:


    =GetServerResponse(A1)


    and just copy down as required.

  • Re: VBA Code to Validate URL and Highlight Cells


    Hi SS,
    Thanks for your quick response. I am very new to VBA and I was using a code from one of existing sheet I have for this purpose.


    The problem with the below code is even though the URL is redirecting the cell will getting highlighted in green. I want the redirected cell to get highlighted in light grey.


    Can you able to modify it? Also, I am guessing the line ".Option(WinHttpRequestOption_EnableRedirects) = False" was not supposed to open the redirecting URL but, I believe it is opening the URL by omitting above command.



    Thanks

  • Re: VBA Code to Validate URL and Highlight Cells


    The problem is that you will only get a redirect response (code 301 for example) if whoever setup the redirect on the server write that into the rule. There is no way of knowing if that has been done without having access to the production server (which I'm assuming you don't have) so if they have set up a straight redirect with no response code then you wouldn't know if there was a redirect - you would get a standard HTTP 200 response.


    As far as this line of code is concerned:


    Code
    .Option(WinHttpRequestOption_EnableRedirects) = False


    I suspect that if you put "Option Explicit" at the top of that module you will get a compile error because that variable hasn't been defined. The code you're using looks like it is creating the request via late binding, but "WinHttpRequestOption_EnableRedirects" looks like a constant that can only be used with early binding.


    Try changing that line to:


    Code
    .Option(6) = False


    and see if that improves it.


    The WinHttpRequestOption enum is as follows:


Participate now!

Don’t have an account yet? Register yourself now and be a part of our community!