我现在对Excel更加熟悉,但有一点让我感到困惑 - 如何在循环中循环访问URL。我目前的难题是我有这个包的URL列表,需要使用它的HTML获取每个页面上每个包的状态。我目前在列表中循环的是:
Sub TrackingDeliveryStatusResults()
Dim IE As Object
Dim URL As Range
Dim wb1 As Workbook, ws1 As Worksheet
Dim filterRange As Range
Dim copyRange As Range
Dim lastRow As Long
Set wb1 = Application.Workbooks.Open("\\S51\******\Folders\******\TrackingDeliveryStatus.xls")
Set ws1 = wb1.Worksheets("TrackingDeliveryStatusResults")
Set IE = New InternetExplorer
With IE
.Visible = True
For Each URL In Range("C2:C & lastRow")
.Navigate URL.Value
While .Busy Or .ReadyState <> 4: DoEvents: Wend
MsgBox .Document.body.innerText
Next
End With
End Sub
我的目标是:
我的理解是我必须为每个不同的URL编写不同的if语句,因为它们的传递状态都有不同的HTML标记。循环很简单,但循环浏览网页对我来说是新的。无论我做出什么改变,代码都给我带来了错误。
答案 0 :(得分:1)
好的,我会以适当的语法开始让你的代码继续下去,我将编辑这个答案以获取更多代码
Sub Sample()
Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.EnableEvents = True
Dim wsSheet As Worksheet, Rows As Long, links As Variant, IE As Object, link As Variant
Set wb = ThisWorkbook
Set wsSheet = wb.Sheets("Sheet1")
Set IE = New InternetExplorer
Rows = wsSheet.Cells(wsSheet.Rows.Count, "A").End(xlUp).Row
links = wsSheet.Range("A1:A" & Rows)
With IE
.Visible = True
For Each link In links
.navigate (link)
While .Busy Or .ReadyState <> 4: DoEvents: Wend
MsgBox .Document.body.innerText
Next link
End With
Application.Calculation = xlCalculationAutomatic
Application.ScreenUpdating = True
Application.EnableEvents = True
End Sub
这将让你循环我认为你有一些一般的语法问题,你可以看到我的代码中的差异,以循环通过为每个链接必须是类型对象或变体和链接我设置为变体假设它将默认为字符串