TPJ87 TPJ87 - 1 month ago 9
HTML Question

How to write if function for more than one row on website table HTML

DataGridReservations is a table. I am running a search by row.

My VBA works for one row. Is Is there a way to loop this VBA code to search each row in the table? Maybe by increasing the Item(2) in xobj1 = .... by 1 for each loop?

Dim xobj1 As Object
Set xobj1 = IE.Document.getElementById("DataGridReservations").getElementsByTagName("tr").Item(2)
Set xobj1 = xobj1.getElementsByTagName("td")
If Range("B1").Text = xobj1.Item(9).innertext Then
Range("H" & (ActiveCell.Row)) = xobj1.Item(3).innertext
Else
Range("H" & (ActiveCell.Row)) = "0"
End If


REWRITE AFTER COMMENTS

I'm now getting an object does not support property or method

Dim xobj1 As Object
Dim xobj2 As Object
Dim colRows As Object
Set colRows = IE.Document.getElementById("DataGridReservations").getElementsByTagName("tr")
For i = 0 To colRows.Length - 1
Set xobj1 = colRows.Item(i)
Set xobj2 = colRows.getElementsByTagName("td")
If Range("B1").Text = xobj2.Item(9).innertext Then
Range("H" & (ActiveCell.Row)) = xobj2.Item(3).innertext
Else
Range("H" & (ActiveCell.Row)) = "0"
End If
Next

Answer

If you want to loop through the rows with a counter, you can do it like this:

Set colRows = IE.Document.getElementById("DataGridReservations").getElementsByTagName("tr")
For i = 0 to colRows.Length -1
    Set xobj1 = colRows.Item(i)
    'Your code
Next

The index starts at '0'. Therefore the counter runs to '.Length -1'

Edited, following revised question:

Dim xobj1 As Object
Dim xobj2 As Object
Dim colRows As Object

Set colRows = IE.Document.getElementById("DataGridReservations").getElementsByTagName("tr")
    For i = 0 To colRows.Length - 1
        Set xobj1 = colRows.Item(i)
        Set xobj2 = xobj1.getElementsByTagName("td")
        If Range("B1").Text = xobj2.Item(9).innertext Then
        Range("H" & (ActiveCell.Row)) = xobj2.Item(3).innertext
        Else
        Range("H" & (ActiveCell.Row)) = "0"
        End If
    Next