VBA Looping pulling from web

jamescooper

Well-known Member
Joined
Sep 8, 2014
Messages
840
Hello trying to get this to loop:

Code:
Sub WebData_2()


    Dim lastRow     As Long
    Dim x           As Long
    Dim urls        As Variant
    Dim Prices      As Variant


'Create sheet


    lastRow = Sheets("7th Aug 2019").Range("H" & Rows.Count).End(xlUp).Row
    urls = Sheets("7th Aug 2019").Range("H11:H" & lastRow).Value


    For x = LBound(urls) To UBound(urls)
        Prices = getprices(urls(x, 1))
        Sheets("7th Aug 2019").Cells(Sheets("7th Aug 2019").Rows.Count, 1).End(xlUp).Offset(1).Resize(UBound(Prices), 17).Value2 = Prices
    Next x
    
End Sub


Private Function getprices(ByVal URL As String) As Variant


    Dim source As Object
    Dim http As New XMLHTTP60, html As New HTMLDocument
      
    With http
        .Open "GET", URL, False
        .send
        html.body.innerHTML = .responseText
    End With


Sheets("7th Aug 2019").Range("F5").Value2 = html.querySelector(".price-details--wrapper .value").innerText
Sheets("7th Aug 2019").Range("G5").Value2 = html.querySelector(".price-per-quantity-weight .value").innerText


End Function

Any ideas please?
 
That is almost there now! Thanks! So now it is putting it all in the same column.

So:

Code:
    ret(2) = html.querySelector(".price-per-quantity-weight .value").innerText

This bit needs to go in the column adjacent to ret(1).

So column F, i.e. 6 has ret(1), column G, i.e. 7 has ret(2).

Does that make sense?

Thanks a lot learning lots here!
 
Upvote 0

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand
In that case, the first one should do what you want...

Code:
[COLOR=#333333]...Resize(, UBound(Prices)).Value2 = Prices[/COLOR]

Did you try it?
 
Upvote 0
Fantastic - this worked, thanks a lot!

Code:
Sub Get_Prices()
    
    Dim lastRow     As Long
    Dim x           As Long
    Dim urls        As Variant
    Dim Prices      As Variant


'Create sheet


    lastRow = Sheets("7th Aug 2019").Range("H" & Rows.Count).End(xlUp).Row
    urls = Sheets("7th Aug 2019").Range("H11:H" & lastRow).Value


    For x = LBound(urls) To UBound(urls)
        Prices = getprices(urls(x, 1))
    Sheets("7th Aug 2019").Cells(Sheets("7th Aug 2019").Rows.Count, 6).End(xlUp).Offset(1).Resize(, UBound(Prices)).Value2 = Prices
    Next x
    
End Sub


Private Function getprices(ByVal URL As String) As Variant


    Dim source As Object
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim ret(1 To 2) As String
    
    With http
        .Open "GET", URL, False
        .send
        html.body.innerHTML = .responseText
    End With
    
    ret(1) = html.querySelector(".price-details--wrapper .value").innerText
    ret(2) = html.querySelector(".price-per-quantity-weight .value").innerText
 
getprices = ret


End Function
 
Upvote 0
Ok So I wish for this code to do something else:

Ignore and skip to the next if a ret(1) or ret(2) if the URL webpage pulls cannot find anything?

Many thanks.


Code:
Sub Get_Prices()
    
    Dim lastRow     As Long
    Dim x           As Long
    Dim urls        As Variant
    Dim Prices      As Variant


'Create sheet


    lastRow = Sheets("7th Aug 2019").Range("H" & Rows.Count).End(xlUp).Row
    urls = Sheets("7th Aug 2019").Range("H11:H" & lastRow).Value


    For x = LBound(urls) To UBound(urls)
        Prices = getprices(urls(x, 1))
    Sheets("7th Aug 2019").Cells(Sheets("7th Aug 2019").Rows.Count, 6).End(xlUp).Offset(1).Resize(, UBound(Prices)).Value2 = Prices
    Next x
    
End Sub


Private Function getprices(ByVal URL As String) As Variant


    Dim source As Object
    Dim http As New XMLHTTP60, html As New HTMLDocument
    Dim ret(1 To 2) As String
    
    With http
        .Open "GET", URL, False
        .send
        html.body.innerHTML = .responseText
    End With
    
    ret(1) = html.querySelector(".price-details--wrapper .value").innerText
    ret(2) = html.querySelector(".price-per-quantity-weight .value").innerText
 
getprices = ret


End Function
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,184
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

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