本文介绍了插入一个HTML表格单元格到没有ID的Excel表格单元格中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我试图通过使用函数将HTML表格中的单元格提取到excel单元格中。表是这样的:
i was trying to extract a cell from a HTML table into a excel cell by using a function. the table is like this:
| 1Q | 2Q | 3Q |
income | 23 | 34 | 22 |
expenses | 11 | 19 | 10 |
.
.
.
我无法通过id获取元素,所以我创建了两个循环来查找元素。代码可以找到元素(在我的例子中,单词费用col1 row2),但我不知道如何获得右边的单元格值(在这种情况下为11)
i cannot get the elements by id, so i created two loops to look for element by element. the code can find the elements (in my case, the word "expenses" col1 row2) but i don't know how to get the cell value to the right (11 in this case)
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
提前致谢
thanks in advance
推荐答案
Scott是没有理由循环遍历所有tds的。
Scott is right there is no reason to loop through all the tds.
For Each tr In colTR
If tr.Cells(0).innerText = "expenses" Then
TheValueIWant = tr.Cells(1).innerText
MsgBox TheValueIWant
End If
Next
这篇关于插入一个HTML表格单元格到没有ID的Excel表格单元格中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!