Home » excel » excel – Retrieve information from all the URLs in column

excel – Retrieve information from all the URLs in column

Posted by: admin May 14, 2020 Leave a comment


I have a code below that uses xml.
It visits all the URLs in column J, gets information, then pastes it to column K.

The code does not seem to work.

Sub GetInfo()
    Dim Http As New XMLHTTP60, Html As New HTMLDocument
    Dim lastrow As Long, i As Long
    Dim sdd As String
    Dim add As Variant
    Dim url As Range

    For Each url In Range("J2:J" & Range("J" & Rows.Count).End(xlUp).Row)
        lastrow = Sheet1.Cells(Rows.Count, "J").End(xlUp).Row
        For i = 2 To lastrow

            With Http
                .Open "GET", url, False
                Html.body.innerHTML = .responseText
            End With
            sdd = Html.querySelector("span[itemprop='price']").getAttribute("content")
            Sheet1.Cells(i, "K") = sdd
        Next i
End Sub

The macro is now able to run. I don’t why its running all of a sudden.
But its still not doing what it is supposed to.
If there are 10 URLs in column J, the macro will get the info from the first URL, then put it on all rows in column K instead of just putting it on its respective row. If the value from the first URL is 80000, it would put 80000 on all 10 rows. Not only that, it would move on to the next URL, then put the new info on all of the rows in column K by replacing what it had just put there. It would do that until it has gone all the way through the 10th URL.

How to&Answers:

Your problem is that you are not indexing into the K column properly. You have a For I loop which repeats for every URL. So the last URL will fill all of the K column entries. Perhaps something like:

    i = 1
    For Each URL In Range(Cells(2, "J"), Cells(Rows.Count, "J").End(xlUp))
        With Http
            .Open "GET", URL, False
            HTML.body.innerHTML = .responseText
        End With

        sdd = HTML.querySelector("span[itemprop='price']").getAttribute("content")
        i = i + 1
        Sheet1.Cells(i, "K") = sdd

    Next URL
End Sub

Oh, and ensure that your URL’s are fully formed: eg that they start with http:// or https://