插入HTML表格单元格到没有ID的Excel工作表单元格
问题描述:
我试图通过使用函数将HTML表格中的单元格提取到Excel单元格中。表是这样的:插入HTML表格单元格到没有ID的Excel工作表单元格
| 1Q | 2Q | 3Q |
income | 23 | 34 | 22 |
expenses | 11 | 19 | 10 |
.
.
.
我不能通过ID获取元素,所以我创建了两个循环来寻找元素的元素。该代码可以找到的元素(在我的情况中,“费用” COL1 2行),但我不知道如何(在这种情况下11)获取单元格的值向右提前
Dim IE As Object
Dim doc As Object
Dim colTR As Object
Dim colTD As Object
Dim tr As Object
Dim td As Object
Set IE = CreateObject("InternetExplorer.Application")
IE.Visible = True
IE.Navigate "www.mywebpage.com"
Do Until IE.ReadyState = 4
DoEvents
Loop
Set doc = IE.Document
Set colTR = doc.GetElementsByTagName("TR")
For Each tr In colTR
Set colTD = tr.GetElementsByTagName("TD")
For Each td In colTD
If td.innertext = "expenses" Then
TheValueIWant = tr.item(1).innertext
End If
Next td
Next tr
IE.Quit
Set IE = Nothing
Set doc = Nothing
Set colTR = Nothing
Set colTD = Nothing
Set td = Nothing
Set tr = Nothing
感谢
答
斯科特是没有理由循环所有的tds。
For Each tr In colTR
If tr.Cells(0).innerText = "expenses" Then
TheValueIWant = tr.Cells(1).innerText
MsgBox TheValueIWant
End If
Next
+0
真棒!它完美的作品 –
+0
高兴地协助。先生! – 2016-07-07 04:13:39
11只是更多的专栏。从您的示例数据中很难说,但可能是1或2或更多。你可以用'debug.print'做一个循环,以便第一次找出它,而不是相应地调整代码。因此,如果它是第二列,它将是'tr.Cells(2).innertext'。 –