使用VBA将特定表格获取到excel中
我正在寻找一种使用VBA将表格转换成excel的方法。由于特定原因,使用web查询不适合 到目前为止,我已经得到了这个使用VBA将特定表格获取到excel中,vba,innerhtml,Vba,Innerhtml,我正在寻找一种使用VBA将表格转换成excel的方法。由于特定原因,使用web查询不适合 到目前为止,我已经得到了这个 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.getel
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
打开的网页中有一个名为“最大导出限制”的表,位于页面的中间和左侧
我找到了负责表格的html代码部分,并设法手动将内部html复制到网页(仅需复制和粘贴),但是如何通过VBA>
@罗恩
到目前为止,我有这一切
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
当您单击“go_按钮”时,将打开一个新的网页,但您的宏仍将集中在原始网页上。在“单击”行后插入以下内容。这段代码将找到新的网页并将焦点放在上面
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
感谢您的帮助,我如何获取sheet1的a1单元格中的数据?我编辑了我的答案,将html放入sheet1的a1单元格中。Hi Ron可以,尽管代码似乎没有返回数据,但不确定出了什么问题。到目前为止,我已经在我最初的帖子中得到了完整的代码。我看到数据放在一个单元格中,一个包含所有代码的列表,以及所有其他内容,我想我只能得到表格?不是这样吗?@Ingram你没有正确复制我的代码。例如,“Exit For”在代码中的位置错误。无论如何,我已经重新编辑了我的代码;我只更改了底部的内容。此代码将仅从“最大导出限制”表中提取html(顺便说一句,如果您将ie.Document.getElementsByTagName(“tbody”)(2.innerhtml更改为ie.Document.getElementsByTagName(“tbody”)(2.innertext,您将获得表文本而不是表html-您想要哪个?)。我在末尾添加的代码将html拆分为单独的行,并将每行放在单独的单元格中