1 Attachment(s)
VBA IE Scrape data to excel
Hello,
Im running a project where VBA is reading values from a column A (Row by row) and with a web page request i want to return some data to excel.
This is automated with the below code
Code:
Sub Query()
Dim x As Integer
Dim i As Integer
NumRows = Range("A1", Range("A1").End(xlDown)).Rows.Count
Range("A1").Select
' Establish "For" loop to loop "numrows" number of times.
For x = 2 To NumRows + 1
'If Len(Range("a" & x)) <> 13 Then
'MsgBox "wrong tracking code"
'Exit Sub
'End If
Dim Xrange As String
Dim t As Integer
Xrange = Trim(Range("A" & x).Value)
Dim ie As New InternetExplorer
t = 2
'web scrape + cell value
2 Application.wait (Now + TimeValue("0:00:002"))
ie.navigate "https://www.elta-courier.gr/search?br=" & Xrange
If Xrange = Mid(Xrange, 1, 2) = "pd" Then t = t - 2
Do While ie.Busy Or ie.readyState <> 4: DoEvents: Loop 'Until ie.readyState = READYSTATE_COMPLETE
Dim Doc As HTMLDocument
Set Doc = ie.document
Dim sDD As String
Dim sDD1 As String
Dim sDD2 As String
'check if page has loaded with data, counting Elements number.
i = Doc.getElementsByTagName("td").Length
If i = 0 Then GoTo 2
sDD = Trim(Doc.getElementsByTagName("TD")(t + 2).innerText)
sDD1 = Trim(Doc.getElementsByTagName("TD")(t).innerText)
sDD2 = Trim(Doc.getElementsByTagName("TD")(t + 1).innerText)
' write values to excel
ie.Quit
Dim aDD As Variant
aDD = Split(sDD, ",")
Range("C" & x).Value = aDD
Range("D" & x).Value = sDD1
Range("E" & x).Value = sDD2
1 Next
Application.ScreenUpdating = True
ie.Quit
End Sub
Data requested to be returned is the second row from the attached picture.
Attachment 27881
PROBLEM
1) I get an error "The object invoked has disconected from its clients
2) It takes to long to read 500 records and parse and receive the data back to excel.
Can someone check what im doing wrong?