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

时间:2014-07-20 23:50:11

标签: sql excel vba 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

1 个答案:

答案 0 :(得分:0)

当然有。你需要拉哪个表?您可以通过索引位置识别它:

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循环内开发一些其他逻辑测试。注意:这是一个非常简单的伪代码示例。您可能需要更复杂的逻辑/条件来确定哪个表是集合中的正确表:

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