Need help rewriting Excel VBA script

I found some script that will check a URL and return the updated link (if a redirect), or identify as a broken link, etc. Problem is it only does one at a time. Can someone help me rewrite this code so it checks against any weblink in Column A, and enters the Redirect Type in B, and New Location in C?

Private Sub RedirectChecker(ByVal url As String)
    Dim sh As Worksheet
    Set sh = ActiveSheet
    
    Dim http As New WinHttp.WinHttpRequest
    http.Option(WinHttpRequestOption_UserAgentString) = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
    http.Option(WinHttpRequestOption_EnableRedirects) = False
    
    '' Clear existing info
    sh.Cells(3, 3).Formula = ""
    sh.Cells(4, 3).Formula = ""
    DoEvents
    
    '' Add protocol if missing
    If (InStr(url, "://") = 0) Then
        url = "http://" & url
    End If
    
    '' Launch the HTTP request
    http.Open "GET", url
    If Err.Number <> 0 Then
        '' Handle URL formatting errors
        sh.Cells(3, 3).Formula = Trim(Err.Description)
        Exit Sub
    End If
    http.Send
    If Err.Number <> 0 Then
        '' Handle HTTP errors
        sh.Cells(3, 3).Formula = Trim(Err.Description)
        Exit Sub
    End If
    '' Show HTTP response info
    sh.Cells(3, 3).Formula = http.Status & " " & http.StatusText
    sh.Cells(4, 3).Formula = http.GetResponseHeader("Location")
End Sub

Open in new window

N2012Asked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

Robberbaron (robr)Commented:
so the code works as expected but only exports results to column C, row 3 & 4

can you post a sample workbook of what results you want to see as it's not completely clear to me.

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
N2012Author Commented:
Thanks anyhow. Couldn't wait any longer and managed to get local help from someone in the office. Here's what I wanted to do:

Private Changing As Boolean

Private Sub RedirectChecker(ByVal url As Range)
    Dim sh As Worksheet
    Set sh = ActiveSheet
    Dim x As Integer
    Dim i As Integer
    Dim http As New WinHttp.WinHttpRequest
    
    http.Option(WinHttpRequestOption_UserAgentString) = "Mozilla/4.0 (compatible; MSIE 7.0; Windows NT 6.0)"
    http.Option(WinHttpRequestOption_EnableRedirects) = False
    
    '' Clear existing info
    x = 2
    sh.Cells(1, 3).Value = "URLS:"
    sh.Cells(1, 4).Value = "Status Code:"
    sh.Cells(1, 5).Value = "New Location:"
    Do While (sh.Cells(x, 4).Formula <> "" And sh.Cells(x, 5).Formula <> "")
        sh.Cells(x, 4).Formula = ""
        sh.Cells(x, 5).Formula = ""
        x = x + 1
    Loop
    DoEvents
    
    i = 2
    For Each link In url
        
        '' Will exit if it comes across an empty cell
        If (Len(link.Value) < 1) Then
            Exit Sub
        End If
        '' Add protocol if missing
        If (InStr(link.Value, "://") = 0) Then
            link.Value = "http://" & link.Value
        End If
        
        '' Launch the HTTP request

        http.Open "GET", link.Value
        If Err.Number <> 0 Then
            '' Handle URL formatting errors
            sh.Cells(i, 3).Formula = Trim(Err.Description)
            Exit Sub
        End If
        http.Send
        If Err.Number <> 0 Then
            '' Handle HTTP errors
            sh.Cells(i, 3).Formula = Trim(Err.Description)
            Exit Sub
        End If
            
        '' Show HTTP response info
        sh.Cells(i, 4).Formula = http.Status & " " & http.StatusText
        If (http.Status < 300 Or http.Status > 399) Then
            sh.Cells(i, 5).Formula = link.Value
        Else
            sh.Cells(i, 5).Formula = http.GetResponseHeader("Location")
        End If
    
    i = i + 1
    Next link
    
End Sub

Private Sub CommandButton1_Click()
    Lastrow = Cells(3, 1).End(xlDown).Row
    RedirectChecker Range("C2:C" & Lastrow)
End Sub

Open in new window

It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
VB Script

From novice to tech pro — start learning today.