I have a HTML table something like this:
Header1 | Value1> |
---|---|
Header2 | Value2> |
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