通过HTML表格元素循环/变量的问题

时间:2016-01-25 17:08:55

标签: html excel excel-vba for-loop vba

我正在开展这个项目,其中涉及从Excel工作表上的URL列表中跟踪单个货件的订单状态。目前,代码能够很好地遍历URL并从中提取信息,但是当我尝试为每个不同的URL添加异常和变量时,我得到了类型不匹配。目前,我需要3个变量来从HTMLCollection / Table中提取HTML信息。我在这里尝试做的是:

  • 遍历每个网址(在IE中插入网址并继续操作而不打开新标签页)
  • 从HTML元素
  • 获取每个网址的项目状态
  • FedEx:已交付(td class =" status")
  • UPS:已发送(id =" tt_spStatus")
  • USPS:抵达USPS设施(等级="首先是信息文本)

我的代码:

Sub TrackingDeliveryStatusUpdate()

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = True

Dim rngURL As Range
Dim wb1 As Workbook, ws1 As Worksheet, ws2 As Worksheet
Dim MyURL As String
Dim Rows As Long, links As Variant, IE As InternetExplorer, link As Variant
Dim i As Long
Dim sID As String
Dim rngLinks As Range, rngLink As Range


Dim filterRange As Range
Dim copyRange As Range
'Dim doc As Object

Set wb1 = Application.Workbooks.Open("\\S51\Store51\Employee Folders\Jason\TrackingDeliveryStatus.xls")
Set ws1 = wb1.Worksheets("TrackingDeliveryStatusResults")
'Set rngURL = ws1.Range("C2:C" & lastRow)
'Arr = rngURL
Set IE = New InternetExplorer

Rows = ws1.Cells(ws1.Rows.Count, "A").End(xlUp).Row
Set rngLinks = ws1.Range("C2:C" & Rows)

'Loop through each used cell in column C on sheet URLs
'For Each rngURL In Worksheets("TrackingDeliveryStatusResults").Range("C2", Worksheets("TrackingDeliveryStatusResults").Range("C" & Rows.Count).End(xlUp))
'Set HTMLDoc = IE.Document
With IE
    .Visible = True
    i = 2
    For Each rngLink In rngLinks
        .Navigate (rngLink)
        While .Busy Or .ReadyState <> 4: DoEvents: Wend
        'If InStr(1, URL.Value, "fedex") Then sID = "status"
                Dim doc As Object
                Set doc = IE.Document
                'I know this line right below is a big mess. Not sure what to do here.
                If InStr(1, rngLink.Value2, "ups") Then
                    sID = "tt_spStatus"
        'If InStr(3, URL.Value, "usps") Then sID = "info-text first"

                    ws1.Range("D" & i).Value = doc.getElementById(sID).Items(0).Value
        'ws1.Range("D" & i).Value = .Document.body.innerText
                End If

    Next rngLink
    i = i + 1
End With


Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True



End Sub

我认为这样可行,但显然它不会引用Excel电子表格中的网址。enter image description here

2 个答案:

答案 0 :(得分:1)

您应该将LinkLinks调暗为范围并更改:

links = ws1.Range("C2:C" & Rows)

为:

Set Links = ws1.Range("C2:C" & Rows)

你试图使Variant等于一个不可能发生的范围。但是,一旦您将Links设置为单元格范围,那么当您循环遍历每个单元格时,请使用link.Value2引用该网址。

答案 1 :(得分:1)

原因不起作用是因为你的陈述中混淆了几个概念。我重构了代码的相关部分,并对变量类型和名称更改提出了一些建议

Dim rngLinks as Range, rngLink as Range

Set rng Links = ws1.Range("C2:C" & Rows)

For each rngLink in rngLinks

只是因为你在这里循环遍历工作表范围内的单元格。

然后在For each rngLink in rngLinks语句中,在代码等待IE加载的语句之后。

Dim doc as Object
Set doc = .document

If InStr(1, rngLink.Value2, "ups") Then 
     sID = "tt_spStatus"
     ws1.Range("D" & I).Value = doc.getElementById(sID).Items(0).Value
End If

您也可以删除For Each TDelement In sID块。这不是必需的。