我如何循环遍历一系列单元格并从网站获取它们的价值?

How can I loop through a range of cells and get a value to them from a website?

我试图从我的工作场所使用的网站获取一些特定数据,但是,我无法弄清楚如何将一些特定数据写入工作表。我写了一段代码,它成功打开网页并循环访问该页面上的数据,我只需要代码的最后一部分。

Dim Source As Range
        Dim Cell As Range

lastRow = Worksheets("Sheet1").Range("A1000").End(xlUp).Row
Set Source = Worksheets("Sheet1").Range("A2:A" & lastRow)

For Each Cell In Source


Dim nodes As Object, i2 As Long

Set nodes = objIE.Document.querySelectorAll("[Title='Purchase Order / Status']")

For i2 = 0 To nodes.Length - 1

    If nodes.Item(i2).innerText Like "*" & Cell.Value & "*" Then
        Cell.Offset(0, 3).Value = ' Im missing the code here
        Exit For
    End If

Next
Next

我遗漏了代码的最后一部分(在上面的代码中标记为遗漏)。此代码循环遍历我的工作表,然后是整个网页,并查找标题为 'Purchase Order / Status' 的数据。这部分代码没问题。

然后我尝试了Cell.Offset(0, 3).Value = objIE.Document.querySelector("[Title='Planned Destinations ']").innerText,成功得到了我需要的'Planned Destinations' innertext值。但是,它总是在整个页面上找到第一个 'Planned Destinations' 内文。

这是它的功能和我想要它做的图片:

注意 D 列。

这是网站的一段:

<tr class="outboundPlanAltRowStyle">
<td class="outboundPlanHour" style="height:25px;width:40px;white-space:nowrap;">11:00</td>
 <td onmouseover="this.className='outboundPlanHover'" onmouseout="this.className=''" 
     onclick="cellClicked(1019543,14)" 
     style="height:25px;width:150px;white-space:nowrap;" class="">
<table class="outboundPlan_PREBOOKED" style="width: 200px; table-layout: fixed" cellpadding="0px" cellspacing="0px">
   <tbody><tr>
    <td title="Purchase Order / Status" class="outboundCell"> 325839 / PREBOOKED</td></tr>
    <tr><td title="Subcontractor Name / Load Numbers " 
               class="outboundCell">Tesco FM /  - </td></tr>
    <tr><td title="Planned Destinations " class="outboundCell" 
       style="overflow: hidden"> 39019 (NDC Teresin)&nbsp;</td></tr>
    <tr><td title="Status Date" class="outboundCell">28.01.2021 12:02&nbsp;</td></tr></tbody>
</table></td>

我需要通过查找保存在工作表(上图中的 A 列)中的 title="Purchase Order / Status" innertext 将 title="Planned Destinations " innertext 值添加到单元格

编辑:

终于成功了。使用不同的方法来解决我的问题,但如果没有蒂姆的提示和帮助,我将无法完成它。这是解决我的问题的最终有效代码,以备不时之需或启发:

Dim objResultList As MSHTML.IHTMLDOMChildrenCollection
        Dim lResultCount As Long
        Dim lResultLoop As Long
        Dim anchorLoop As MSHTML.HTMLAnchorElement

Set objResultList = objIE.Document.querySelectorAll("[Title='Planned Destinations ']")
    
    lResultCount = objResultList.Length

    Debug.Print
    
    For lResultLoop = 0 To lResultCount - 1
        
        Set anchorLoop = objResultList.Item(lResultLoop)
     
    Next
        
    i3 = 0
    Set Source = Worksheets("Sheet1").Range("D2:D" & lastRow)
    If Not anchorLoop Is Nothing Then
    For Each Cell In Source
    Cell.Value = objResultList.Item(i3).innerText
    i3 = i3 + 1
    
    Next
    End If
    End If

您需要从 nodes.Item(i2)“向上”到父 table(使用例如 parentElement),然后查询 table(不是整个文档)对于您想要的其他单元格:

Sub tester()
    'Added reference to Microsoft HTML Object Model
    Dim doc As New HTMLDocument, nodes As Object, i As Long
    Dim tbl As HTMLTable, nxt
    
    doc.body.innerHTML = Range("A1").Value 'load HTML from cell for testing

    Set nodes = doc.querySelectorAll("td [Title='Purchase Order / Status']")
    Debug.Print "found " & nodes.Length & " cell(s)"
    
    For i = 0 To nodes.Length - 1
    
        'get the parent table
        Set tbl = nodes(i).parentElement.parentElement.parentElement
        'find the other cell in this table
        Set nxt = tbl.querySelector("td [Title='Planned Destinations ']")
        
        Debug.Print nodes(i).innerText, nxt.innerText
        
    Next i
    
End Sub

HTML 在 A1:

<table><tr>
      <td>11:00</td>
     <td>
             <table class="outboundPlan_PREBOOKED">
            <tbody>
                    <tr><td title="Purchase Order / Status" class="outboundCell"> 325839 / PREBOOKED</td></tr>
                   <tr><td title="Subcontractor Name / Load Numbers "  class="outboundCell">Tesco FM /  - </td></tr>
                   <tr><td title="Planned Destinations " class="outboundCell" >39019 (NDC Teresin)&nbsp;</td></tr>
                   <tr><td title="Status Date" class="outboundCell">28.01.2021 12:02&nbsp;</td></tr>
            </tbody>
            </table></td>
</tr></table>