randaubienghoc
New Member
- Joined
- Apr 25, 2020
- Messages
- 19
- Office Version
- 2016
- Platform
- Windows
Hi guys,
I am looking for your kind help on my coding to pull data from Web to Excel.
Web to get data: https://eport.saigonnewport.com.vn/Pages/Common/Containers_new
Put container number to field "Container"
Deselect "Chỉ vòng luân chuyển cuối" to show all rows in data table
Click Search to show data table - result for searching Container information
*****ISSUE: Data scraped from web to Excel for each row in Excel (respective to each container number found) seems to be the same with the previous result WHILE information for this container number can be blank. For example: Event time 2 "10/4/2020 3:07:00 PM" is repeated for container "TEMU3311320" while this container does not have Event time 2.
Hope you could give me any advices to resolve this duplicating issue. Attached Excel file for your references. Thanks. Link download Excel file example
I am looking for your kind help on my coding to pull data from Web to Excel.
Web to get data: https://eport.saigonnewport.com.vn/Pages/Common/Containers_new
- Steps to get data:
Put container number to field "Container"
Deselect "Chỉ vòng luân chuyển cuối" to show all rows in data table
Click Search to show data table - result for searching Container information
*****ISSUE: Data scraped from web to Excel for each row in Excel (respective to each container number found) seems to be the same with the previous result WHILE information for this container number can be blank. For example: Event time 2 "10/4/2020 3:07:00 PM" is repeated for container "TEMU3311320" while this container does not have Event time 2.
Hope you could give me any advices to resolve this duplicating issue. Attached Excel file for your references. Thanks. Link download Excel file example
VBA Code:
Sub PullDataFromWeb()
Dim IE As Object, W As Excel.Worksheet
Dim doc As HTMLDocument
Dim lastRow As Integer, b As Boolean, tmp As String
Dim lis, li
Set W = ThisWorkbook.Sheets("Sheet1")
Set IE = VBA.CreateObject("InternetExplorer.Application")
IE.Visible = True 'hien cua so IE
IE.navigate "https://eport.saigonnewport.com.vn/Pages/Common/Containers_new"
Do While IE.Busy Or IE.readyState <> 4 'doi IE chay xong
Application.Wait DateAdd("s", 1, Now)
Loop
Set doc = IE.document
lastRow = W.Range("B" & W.UsedRange.Rows.Count + 2).End(xlUp).Row 'dong cuoi cung trong cot B container
If lastRow < 2 Then GoTo Ends
On Error Resume Next
For intRow = 2 To lastRow 'tu dong toi dong
b = False
b = W.Range("I" & intRow).Value Like "[Yy]"
If W.Range("B" & intRow).Value <> "" And Not b Then
doc.getElementById("txtItemNo_I").Value = W.Range("B" & intRow).Value 'so cont
doc.getElementById("cbSite_VI").Value = W.Range("A" & intRow).Value
doc.getElementById("chkInYard_I").Checked = False
doc.getElementById("ContentPlaceHolder2_btnSearch").Click 'click Search
'----------------------------------------------
Do While IE.Busy Or IE.readyState <> 4
Application.Wait DateAdd("s", 1, Now)
Loop
'----------------------------------------------
strFindContainer = doc.getElementById("ContentPlaceHolder2_lblNotice").innerText
W.Range("H" & intRow) = strFindContainer
If strFindContainer Like "T*m th*y * container*" Then
strEventtime1 = doc.getElementById("grdContainer_DXDataRow0").Cells(0).innerText
strEventtype1 = doc.getElementById("grdContainer_DXDataRow0").Cells(1).innerText
strLocation1 = doc.getElementById("grdContainer_DXDataRow0").Cells(2).innerText
strEventtime2 = doc.getElementById("grdContainer_DXDataRow1").Cells(0).innerText
strEventtype2 = doc.getElementById("grdContainer_DXDataRow1").Cells(1).innerText
W.Range("C" & intRow) _
.Resize(, 5).Value = Array(strEventtime1, strEventtype1, strLocation1, _
strEventtime2, strEventtype2)
End If
End If
Next
Ends:
IE.Quit
Set IE = Nothing 'Cleaning up
Set objElement = Nothing
Set objCollection = Nothing
Application.StatusBar = ""
Application.DisplayAlerts = True
End Sub