Skip to content Skip to sidebar Skip to footer

Compare Index Of 2 Elements In A Collection

Issue : I have some issues figuring out a way to select elements in my HTMLDocument which are under a certain point in the page. In the following code sample, as you can see in th

Solution 1:

Untested but I would do something like this (assuming I understood you correctly)

Sub Tester()

    Const S_MATCH As String = "td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']"

    Dim e, tbl, bHit As Boolean

    '...
    'load page etc
    '...


    'get all the matching rows and cycle though them
    For Each e In IEDoc.querySelectorAll(S_MATCH)

        'did we get to the table of interest yet?
        If Not bHit Then
            Set tbl = e.ParentNode.ParentNode.ParentNode.ParentNode. _
                        ParentNode.ParentNode.ParentNode
            If IsPartUsageTable(tbl) Then bHit = True
        End If

        If bHit Then
            'we reached the table of interest, so
            '  do something with e
        End If

    Next

End Sub

Function IsPartUsageTable(tbl) As Boolean
    Dim e, rv As Boolean
    For Each e In tbl.getElementsByClassName("SectionHead")
        If Element.innerHTML = "Part Usage" Then
            rv = True
            Exit For
        End If
    Next
    IsPartUsageTable = rv
End Function

Solution 2:

Ok, so as unexpected as it sounds, I think I found a solution to my own question. I will confirm you that it works as soon as I have the possibility to run it with my colleague. So I keep point 1 and 2 from my initial post and I replaced point 3 with the following :

For i = 0 To IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table").length 
          If IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table")(i).ID = "Stop" Then
                index_Part_Usage = i
                Position_Part_Usage = index + 1
                Exit For
           End If
      Next
       'MsgBox Position_Part_Usage






      For i = 0 To IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table").length
          If IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table")(i).className = "Bim" Then
                index = i
                Position = index + 1
                If index > index_Part_Usage Then
                    For Each Element2 In IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table")(i).querySelectorAll("td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']") ' Now we are in the table which contains the part numbers and we'll look for all the part numbers it contains by applying the queryselectorall again, but this time only in this specific table

                        array_parts2(iteration2) = IEDoc.getElementsByTagName("form")(0).getElementsByTagName("table")(i).querySelectorAll("td[width='100'][class='ListMainCent'][rowSpan='1'][colSpan='1']")(iteration2).innerHTML

                         ActiveWorkbook.Worksheets(1).Cells(iteration2 + 1, 19) = array_parts2(iteration2)

                        iteration2 = iteration2 + 1
                    Next
                End If
           End If
      Next i

Post a Comment for "Compare Index Of 2 Elements In A Collection"