Cómo escribir si funciona para más de una fila en la tabla del sitio web 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

preguntado el 27 de noviembre de 13 a las 04:11

You can loop through the collection returned by the call to getElementsByTagName("tr") (ignoring the first instance). Eg. see: stackoverflow.com/questions/15555513/… -

Right, I was just thinking maybe there was a way to write the 2 as a formula like x+1 where x is the active item number. and just loop through the list until I ran out of tr elements in the table. (1.e the first loop would set item(2), the 2nd loop would set item(3), etc.) -

To reorganize, I also wrote this question stackoverflow.com/q/20255214/3023445 -

1 Respuestas

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

respondido 27 nov., 13:21

inside of your for...next could i Set xobj1 = colRows.Item(i) ? - TPJ87

Yes, of course. Or colRows(i). I should have written that :) - Michael

hmmm. object does not support property or method. see question for my rewrite - TPJ87

Try: Set xobj2 = xobj1.getElementsByTagName("td") - Michael

No es la respuesta que estás buscando? Examinar otras preguntas etiquetadas or haz tu propia pregunta.