问题
I am looking for a way to get a table into excel using VBA. Using a web query is not suitable for specific reasons.
So far i have got this
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://bmreports.com/servlet/com.logica.neta.bwp_PanBMUTop"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.document.getelementbyid("param5").Value = "2014-04-12"
ie.document.getelementbyid("param6").Value = "8"
ie.document.getelementbyid("go_button").Click
The web page that opens has a table within it called Maximum Export Limit - half way down the page and on the left hand side.
I found the part of the html code that is responsible for the table and manged to copy the inner html to the web page manually (just copy and pasting) however how do i do this via VBA>
@Ron So far i have this in all
Sub DataStuff()
Dim ie As Object
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://bmreports.com/servlet/com.logica.neta.bwp_PanBMUTop"
ie.Visible = True
While ie.Busy
DoEvents
Wend
ie.Document.getelementbyid("param5").Value = "2014-04-12"
ie.Document.getelementbyid("param6").Value = "8"
ie.Document.getelementbyid("go_button").Click
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
If my_url Like "http://bmreports.com/servlet/com.logica.neta.bwp_PanBMDataServlet" Then
Set ie = objShell.Windows(x)
table_html = ie.Document.getElementsByTagName("tbody")(2).innerhtml
Worksheets("Sheet1").Activate
Range("A1").Select
ActiveCell = table_html
Exit For
Else
End If
Next
End Sub
回答1:
When you click on the "go_button" a new web page is opened, but your macro remains focused on the original web page. Insert the following after your "click" line. This code will find the new web page and place focus on it
Sub DataStuff()
Set ie = CreateObject("InternetExplorer.Application")
ie.Navigate "http://bmreports.com/servlet/com.logica.neta.bwp_PanBMUTop"
ie.Visible = True
Do Until Not ie.Busy And ie.readyState = 4
DoEvents
Loop
ie.Document.getelementbyid("param5").Value = "2014-04-12"
ie.Document.getelementbyid("param6").Value = "8"
ie.Document.getelementbyid("go_button").Click
Set objShell = CreateObject("Shell.Application")
IE_count = objShell.Windows.Count
For x = 0 To (IE_count - 1)
On Error Resume Next ' sometimes more web pages are counted than are open
my_url = objShell.Windows(x).Document.Location
my_title = objShell.Windows(x).Document.Title
If my_url Like "http://bmreports.com/servlet/com.logica.neta.bwp_PanBMDataServlet" Then
Set ie = objShell.Windows(x)
Exit For
Else
End If
Next
table_html = ie.Document.getElementsByTagName("tbody")(2).innerhtml
html_lines = Split(table_html, Chr(10), -1, vbTextCompare)
Worksheets("Sheet1").Activate
Range("A1").Select
For x = 0 To UBound(html_lines)
ActiveCell = html_lines(x)
ActiveCell.Offset(1, 0).Select
Next
End Sub
来源:https://stackoverflow.com/questions/23048363/get-specific-table-into-excel-using-vba