My code is simple. It navigates different URLs for different worksheets and import two data from each of the URLs.
Public Sub import_Data()
Dim ie As InternetExplorer, htmlDoc As HTMLDocument
Dim price As String, volume As String
Dim sheet As Integer
Dim link(1 to 14) as string, URL As String
'URL to navigate
link(1) = "https://hk.finance.yahoo.com/q?s=0017.HK&ql=0"
link(2) = "https://hk.finance.yahoo.com/q?s=0151.HK&ql=0"
link(3) = "https://hk.finance.yahoo.com/q?s=0267.HK&ql=0"
link(4) = "https://hk.finance.yahoo.com/q?s=0494.HK&ql=0"
link(5) = "https://hk.finance.yahoo.com/q?s=0700.HK&ql=0"
link(6) = "https://hk.finance.yahoo.com/q?s=0857.HK&ql=0"
link(7) = "https://hk.finance.yahoo.com/q?s=0883.HK&ql=0"
link(8) = "https://hk.finance.yahoo.com/q?s=0939.HK&ql=0"
link(9) = "https://hk.finance.yahoo.com/q?s=0992.HK&ql=0"
link(10) = "https://hk.finance.yahoo.com/q?s=1088.HK&ql=0"
link(11) = "https://hk.finance.yahoo.com/q?s=1398.HK&ql=0"
link(12) = "https://hk.finance.yahoo.com/q?s=1880.HK&ql=0"
link(13) = "https://hk.finance.yahoo.com/q?s=3328.HK&ql=0"
link(14) = "https://hk.finance.yahoo.com/q?s=3988.HK&ql=0"
Set ie = New InternetExplorer
ie.Visible = True
For sheet = 1 To 14
Worksheets(sheet).Activate
'Go to different URL for different worksheets
URL = link(sheet)
ie.navigate URL
Do Until ie.readyState = READYSTATE_COMPLETE
DoEvents
Loop
Set htmlDoc = ie.document
'pull two data from url
price = htmlDoc.getElementsByClassName("time_rtq_ticker")(0).innerText
volume = htmlDoc.getElementsByClassName("yfnc_tabledata1")(9).innerText
Cells(22, 2) = price
Cells(22, 3) = volume
Next sheet
End Sub
The weird thing is that for each of the URLs, it can sometimes import the 2 data successfully, but sometimes it gets stuck in the "DoEvents" line.
And I have asked others for help, one reply me that he can run the code smoothly with no error. But for me, I at least stuck in one of the url and cannot proceed.
By the way, the code's efficiency is kind of low.
What's wrong with the code? Why does it works sometimes but does not completely fail? Can anyone help me with this please?
Bookmarks