![]() |
|
#1
|
|||
|
|||
|
Just recently I've posted the same file in this forum and now I've got another example that
the run-time result is just different from what I can get when I step into the codes pressing F8 every time. It drives me crazy cos it wastes all of my time (a few hours) to make sure the codes run well before run-time. When I press F5 to run it all, it just differs and I am totally frustrated here. Please help take a look at the file (see the comments in the codes for the problem) and if you can walk me through this problem, I would be very thankful for your help. It does help me understand VBA far better. Many thanks in advance. |
|
#2
|
|||
|
|||
|
Self solved.
Code:
Sub test()
Application.ScreenUpdating = False
ThisWorkbook.Activate
Worksheets("ApplicationData").Select
Dim IE As Object
Dim HTMLDoc As Object
Dim oRange As Range
Dim pos As Long
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = False
For Each cell In Selection.Cells
IE.Navigate "http://www.chsc.hk/ssp/main.php?lang_id=1"
While IE.readyState < 4
Application.Wait DateAdd("s", 1 / 1000, Now)
Wend
Set HTMLDoc = IE.Document
HTMLDoc.getElementById("sch_name").Value = Cells(cell.Row, 2).Value
HTMLDoc.getElementById("btn_search").Click
Application.Wait DateAdd("s", 1, Now)
For Each tr In HTMLDoc.getElementsByTagName("tr")
If tr.className = "psp_table_line2_bgcolor" Then
IE.Navigate tr.getElementsByTagName("a")(0).href
Exit For
End If
Next
Application.Wait DateAdd("s", 1, Now)
Cells(cell.Row, 3).Value = HTMLDoc.getElementsByTagName("span")(3).innerText
Cells(cell.Row, 4).Value = HTMLDoc.getElementsByTagName("td")(73).innerText
Set oRange = Cells(cell.Row, 4).Find(What:="(", LookAt:=xlPart)
If Not oRange Is Nothing Then
pos = Application.WorksheetFunction.Find("(", Cells(cell.Row, 4).Value) - 1
Cells(cell.Row, 4) = Trim(Left(Cells(cell.Row, 4).Value, pos))
End If
Next cell
IE.Quit
Set IE = Nothing
Application.ScreenUpdating = True
End Sub
Code:
Application.Wait DateAdd("s", 1, Now)
I don't know why waiting for the least of ONE second will get Excel extract data from the page designated. |
|
|
|
Similar Threads
|
||||
| Thread | Thread Starter | Forum | Replies | Last Post |
Word Equation field codes
|
mkarthic | Word | 1 | 12-02-2011 02:09 AM |
Mail merge erases field codes
|
Medievalguy88 | Mail Merge | 1 | 08-11-2011 05:21 AM |
| Convert LISTNUM field codes to text | remedios43 | Word | 2 | 07-31-2011 09:21 AM |
confusion with merge and field codes
|
BluRay | Mail Merge | 5 | 03-29-2011 01:06 AM |
Using Field Codes to Calculate 5 years from a Date
|
vandy | Word | 1 | 09-23-2009 11:32 AM |