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?
 

Excel Facts

Square and cube roots
The =SQRT(25) is a square root. For a cube root, use =125^(1/3). For a fourth root, use =625^(1/4).
Are you getting an error? If so, which one and on which line?
 
Upvote 0
You aren't returning anything from your function, all it will do is overwrite the same 2 cells for every url in your list. You're also resizing a range to 17 columns, where I think you're only wanting to return 2 values from a web request.

I suspect that your function should be like this:
Code:
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
 


End Function


I can't work out what you're trying to do with the returned data though, so I can't help - sorry
 
Upvote 0
Thanks. So this now doesn't return anything and fails on the line:

Code:
Sheets("7th Aug 2019").Cells(Sheets("7th Aug 2019").Rows.Count, 1).End(xlUp).Offset(1).Resize(UBound(Prices), 17).Value2 = Prices

with run-time error 13.

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
    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
 
End Function
 
Upvote 0
The problem may lie in the way you've defined your selector string. For example if .price-details--wrapper and .value are class names, you'll need to remove the space between them so that they are chained together using the dot operator. However, if the former is a class and the latter is an attribute, you'll need to set up your string differently.

If they are both class names, try...

Code:
ret(1) = html.querySelector(".price-details--wrapper.value").innerText

If one is a class name and the other is an attribute, try...

Code:
ret(1) = html.querySelector(".price-details--wrapper[value]").innerText

Does this help?
 
Last edited:
Upvote 0
The problem may lie in the way you've defined your selector string. For example if .price-details--wrapper and .value are class names, you'll need to remove the space between them so that they are chained together using the dot operator. However, if the former is a class and the latter is an attribute, you'll need to set up your string differently.

If they are both class names, try...

Code:
ret(1) = html.querySelector(".price-details--wrapper.value").innerText

If one is a class name and the other is an attribute, try...

Code:
ret(1) = html.querySelector(".price-details--wrapper[value]").innerText

Does this help?


I tried both of those options and I received error code 91 on that line of code.

It may help if I link the 2 URLs as examples:

In H11: https://www.tesco.com/groceries/en-GB/products/303105747
In H12: https://www.tesco.com/groceries/en-GB/products/292285280

Many thanks.
 
Upvote 0
Kyle's code in #3 needs:

Code:
getprices = ret
immediately before the End Function statement.

Thanks, so getting there. It is now pulling the data from the following code twice:

Code:
    ret(1) = html.querySelector(".price-details--wrapper .value").innerText

and not pulling data for:

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

So currently I have:

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, 6).End(xlUp).Offset(1).Resize(UBound(Prices), 1).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

Thanks.
 
Upvote 0
So changing to the 2 in bold from 1, ensures both are pulled. But it is not duplicating it so I get 3.00, 3.00,

Any ideas?

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, 6).End(xlUp).Offset(1).Resize(UBound(Prices), [B]2[/B]).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
Prices get assigned a two-element one-dimensional horizontal array. Therefore, to transfer the contents of the array to a horizontal range of cells, try...

Code:
Sheets("7th Aug 2019").Cells(Sheets("7th Aug 2019").Rows.Count, 6).End(xlUp).Offset(1).Resize(, UBound(Prices)).Value2 = Prices

To transfer the contents to a vertical range of cells, try...

Code:
Sheets("7th Aug 2019").Cells(Sheets("7th Aug 2019").Rows.Count, 6).End(xlUp).Offset(1).Resize(UBound(Prices)).Value2 = Application.Transpose(Prices)
 
Last edited:
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,178
Members
453,021
Latest member
Justyna P

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