如何从Excel VBA中的Internet Explorer中获取某个表

你好我已经search了互联网试图find解决这个问题。

  • 我试图从这个私人网站使用excel-vba拉一个表。
  • 我遇到这个代码,允许我拉所有的表,但我只需要一张表 – 有什么办法可以编辑这只拉一个特定的表?

Sub GetAllTables(doc As Object) ' get all the tables from a webpage document, doc, and put them in a new worksheet Dim IE As Object Dim ws As Worksheet Dim rng As Range Dim tbl As Object Dim rw As Object Dim cl As Object Dim tabno As Long Dim nextrow As Long Dim I As Long Dim oXL As Excel.Application Set ws = Sheet1 For Each tbl In doc.getElementsByTagName("TABLE") tabno = tabno + 1 nextrow = nextrow + 1 Set rng = ws.Range("C" & nextrow) rng.Offset(, -1) = "Table " & tabno For Each rw In tbl.Rows For Each cl In rw.Cells rng.Value = cl.outerText Set rng = rng.Offset(, 1) I = I + 1 Next cl nextrow = nextrow + 1 Set rng = rng.Offset(1, -I) I = 0 Next rw Next tbl ws.Range("C:N").ClearFormats 

当然有。 你需要拉哪张桌子? 您可以通过索引位置来识别它:

 Dim t as Integer 'represents the index position of the table you want to obtain. t = 0 Set tbl = doc.getElementsByTagName("TABLE")(t) 

或者,如果索引位置不知道,则需要在For/Each循环内开发一些其他逻辑testing。 注意:这是一个伪代码非常简单的例子。 您可能需要更复杂的逻辑/条件来确定哪个表是集合中正确的表:

 For each tbl in doc.getElementsByTagName("TABLE") If tbl.__some_property__ = "something" Then Exit For End If Next