使用 VBA 代码从 tr 获取所有 td

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/15555513/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 20:10:38  来源:igfitidea点击:

Get All td from tr using VBA code

vbaexcel-vbaexcel

提问by Kevin Pope

enter image description hereenter image description here

在此处输入图片说明在此处输入图片说明

I have a tr which is a object and has td. I want to get all td in another object variable tblTD. For that i used Set tblTD = tr.getelementsbytagname("td"). But when i check the lenght of tblTD it shows as 0. Can someone suggest how this can be done. Kindly refer the images attached. Thanks!

我有一个 tr,它是一个对象并且有 td。我想在另一个对象变量 tblTD 中获取所有 td。为此,我使用了Set tblTD = tr.getelementsbytagname("td"). 但是当我检查 tblTD 的长度时,它显示为 0。有人可以建议如何做到这一点。请参考所附图片。谢谢!

回答by Kevin Pope

If you've set trwith the getElementsByTagNamefunction, then you'll need to iterate through each tr object to get the child td objects (or you can just reference a single one):

如果你已经设置trgetElementsByTagName函数,那么你需要遍历每个 tr 对象来获取子 td 对象(或者你可以只引用一个):

Dim td As MSHTML.IHTMLElementCollection
Dim tr As MSHTML.IHTMLElementCollection
Dim trObj as MSHTML.HTMLGenericElement
Dim tdObj as MSHTML.HTMLGenericElement

Set tr = HTMLDoc.getElementsByTagName("tr")
For Each trObj In tr
    Set td = trObj.getElementsByTagName("td")
    For Each tdObj in td
        'do something with each td object'
    Next
Next