vba html形式 table,html - Traverse HTMLDOM table with VBA - Stack Overflow

I have a HTML table something like this:

Header1Value1>
Header2Value2>

I want to find the

equal to "Header2" and then return the corresponding , i.e. "Value2", which is inside the same .

I know I can easily use the index number, e.g. getElementsByTagName("td")(1) to find this value, but this is not feasible since each page may have the rows jumbled up.

I've tried doing this varying ways with no success. Hopefully, the following code indicates what I'm trying to do:

Public Declare Function SetForegroundWindow Lib "user32" (ByVal HWND As Long) As Long

Sub WebSearch()

Dim URL As String

Dim IE As Object

Dim HWNDSrc As Long

Dim html As IHTMLDocument

Dim Example As IHTMLElement

Dim TableRows As IHTMLElementCollection

Dim TableRow As IHTMLElement

Dim RowChildren As IHTMLElementCollection

Dim RowChild As IHTMLElement

Dim TableHeader As IHTMLElement

Dim TableData As IHTMLElement

URL = "https://..."

Set IE = CreateObject("InternetExplorer.Application")

IE.Visible = False

IE.Navigate URL

Do While IE.ReadyState = 4: DoEvents: Loop

Do Until IE.ReadyState = 4: DoEvents: Loop

HWNDSrc = IE.HWND

SetForegroundWindow HWNDSrc

Set html = IE.document

On Error Resume Next

Set Example = html.getElementsByClassName("Example")(0)

'''''''' Trying to get Result

Set TableRows = Example.Children

For Each TableRow In TableRows

Set RowChildren = TableRow.Children

For Each RowChild In RowChildren

Set TableHeader = RowChild.getElementsByTagName("th")(0)

Set TableData = TableHeader.NextSibling

If TableHeader.innerText = "Header2" Then MsgBox TableData.innerText

Next

Next

IE.Quit

Set IE = Nothing

Application.StatusBar = ""

End Sub

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值