Excel 使用错误处理程序绕过超链接/url超时
我正在编写一些代码,通过url打开许多文件。这一切都很好,但是过了一段时间,我从中提取数据的服务器阻塞了我,这会抛出一条错误消息 我试图做的是创建一个错误处理程序,重置错误,然后在等待5秒后从顶部继续。我试过两件事Excel 使用错误处理程序绕过超链接/url超时,excel,vba,Excel,Vba,我正在编写一些代码,通过url打开许多文件。这一切都很好,但是过了一段时间,我从中提取数据的服务器阻塞了我,这会抛出一条错误消息 我试图做的是创建一个错误处理程序,重置错误,然后在等待5秒后从顶部继续。我试过两件事 在错误恢复下一步,跳过该行。这似乎没有任何作用,因为代码仍然超时 转到错误处理程序,等待5秒钟,重置错误,然后继续代码所在的位置 知道我做错了什么吗。下面的示例文件路径 谢谢 斯科特一些建议: Option Explicit 'Use Option Explicit Pu
斯科特一些建议:
Option Explicit 'Use Option Explicit
Public Sub RetrieveYahooData()
Const MAX_RETRIES As Long = 3
Dim i As Long, ws As Worksheet, lastRow As Long 'use Long
Dim wbMain As Workbook, wb As Workbook, xUrl As String 'declare xUrl
Dim xtable As String 'temp assignment.
Start 'what subs are these?
Set wbMain = Workbooks("SHARE PRICE CREATOR.xlsb") ''Put in a variable. This assumes is open.
Set ws = wbMain.Worksheets("links")
lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row 'You want to count from row 2 I think
If lastRow >= 2 Then
For i = 2 To lastRow
If i Mod 100 = 0 Then Application.Wait Now + TimeSerial(0, 0, 5) 'every n e.g. 100 requests have a pause
numberOfTries = 0
With ws
xtable = .Cells(i, 5).Value '?What is xTable and its datatype? _
Declare it and use Option Explicit at top of code. _
Also, where will it be used?
xUrl = .Cells(i, 4).Value
If xUrl <> vbNullString Then
Do
DoEvents
On Error Resume Next
Set wb = Workbooks.Open(xUrl, Format:=6, DELIMITER:=",") 'add other tests for valid url?
On Error GoTo 0
If Not wb Is Nothing Then 'remember to save and exit do
wb.SaveAs wbMain.Path & "\" & wb.Name, AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges 'Credit to @Sorceri https://stackoverflow.com/a/14634781/6241235
wb.Close True
Exit Do
Else
Application.Wait Now + TimeSerial(0, 0, 5)
End If
Loop While numberOfTries < MAX_RETRIES
End If
End With
ws.Cells(i, 6) = IIf(wb Is Nothing, "FAIL", "OK")
Set wb = Nothing
Next
End If
ENDING
End Sub
ErrHandle
工作簿(“SHARE PRICE CREATOR.xlsb”).工作表(“links”)
放入一个变量,并用该变量限定您的范围调用Activesheet
引用Err.Clear
清除错误退出子,以成功完成所有任务
GoTo
选项Explicit
强制执行Option Explicit 'Use Option Explicit
Public Sub RetrieveYahooData()
Const MAX_RETRIES As Long = 3
Dim i As Long, ws As Worksheet, lastRow As Long 'use Long
Dim wbMain As Workbook, wb As Workbook, xUrl As String 'declare xUrl
Dim xtable As String 'temp assignment.
Start 'what subs are these?
Set wbMain = Workbooks("SHARE PRICE CREATOR.xlsb") ''Put in a variable. This assumes is open.
Set ws = wbMain.Worksheets("links")
lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row 'You want to count from row 2 I think
If lastRow >= 2 Then
For i = 2 To lastRow
If i Mod 100 = 0 Then Application.Wait Now + TimeSerial(0, 0, 5) 'every n e.g. 100 requests have a pause
numberOfTries = 0
With ws
xtable = .Cells(i, 5).Value '?What is xTable and its datatype? _
Declare it and use Option Explicit at top of code. _
Also, where will it be used?
xUrl = .Cells(i, 4).Value
If xUrl <> vbNullString Then
Do
DoEvents
On Error Resume Next
Set wb = Workbooks.Open(xUrl, Format:=6, DELIMITER:=",") 'add other tests for valid url?
On Error GoTo 0
If Not wb Is Nothing Then 'remember to save and exit do
wb.SaveAs wbMain.Path & "\" & wb.Name, AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges 'Credit to @Sorceri https://stackoverflow.com/a/14634781/6241235
wb.Close True
Exit Do
Else
Application.Wait Now + TimeSerial(0, 0, 5)
End If
Loop While numberOfTries < MAX_RETRIES
End If
End With
ws.Cells(i, 6) = IIf(wb Is Nothing, "FAIL", "OK")
Set wb = Nothing
Next
End If
ENDING
End Sub
我不喜欢GoTos
,因为这会使代码难以阅读和调试。请参阅下面可能的重写,并附上更多注释:
待办事项:
Option Explicit 'Use Option Explicit
Public Sub RetrieveYahooData()
Const MAX_RETRIES As Long = 3
Dim i As Long, ws As Worksheet, lastRow As Long 'use Long
Dim wbMain As Workbook, wb As Workbook, xUrl As String 'declare xUrl
Dim xtable As String 'temp assignment.
Start 'what subs are these?
Set wbMain = Workbooks("SHARE PRICE CREATOR.xlsb") ''Put in a variable. This assumes is open.
Set ws = wbMain.Worksheets("links")
lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row 'You want to count from row 2 I think
If lastRow >= 2 Then
For i = 2 To lastRow
If i Mod 100 = 0 Then Application.Wait Now + TimeSerial(0, 0, 5) 'every n e.g. 100 requests have a pause
numberOfTries = 0
With ws
xtable = .Cells(i, 5).Value '?What is xTable and its datatype? _
Declare it and use Option Explicit at top of code. _
Also, where will it be used?
xUrl = .Cells(i, 4).Value
If xUrl <> vbNullString Then
Do
DoEvents
On Error Resume Next
Set wb = Workbooks.Open(xUrl, Format:=6, DELIMITER:=",") 'add other tests for valid url?
On Error GoTo 0
If Not wb Is Nothing Then 'remember to save and exit do
wb.SaveAs wbMain.Path & "\" & wb.Name, AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges 'Credit to @Sorceri https://stackoverflow.com/a/14634781/6241235
wb.Close True
Exit Do
Else
Application.Wait Now + TimeSerial(0, 0, 5)
End If
Loop While numberOfTries < MAX_RETRIES
End If
End With
ws.Cells(i, 6) = IIf(wb Is Nothing, "FAIL", "OK")
Set wb = Nothing
Next
End If
ENDING
End Sub
使用辅助函数/子函数重构代码,使其嵌套更少,即更模块化
代码:
Option Explicit 'Use Option Explicit
Public Sub RetrieveYahooData()
Const MAX_RETRIES As Long = 3
Dim i As Long, ws As Worksheet, lastRow As Long 'use Long
Dim wbMain As Workbook, wb As Workbook, xUrl As String 'declare xUrl
Dim xtable As String 'temp assignment.
Start 'what subs are these?
Set wbMain = Workbooks("SHARE PRICE CREATOR.xlsb") ''Put in a variable. This assumes is open.
Set ws = wbMain.Worksheets("links")
lastRow = ws.Cells(ws.Rows.Count, "D").End(xlUp).Row 'You want to count from row 2 I think
If lastRow >= 2 Then
For i = 2 To lastRow
If i Mod 100 = 0 Then Application.Wait Now + TimeSerial(0, 0, 5) 'every n e.g. 100 requests have a pause
numberOfTries = 0
With ws
xtable = .Cells(i, 5).Value '?What is xTable and its datatype? _
Declare it and use Option Explicit at top of code. _
Also, where will it be used?
xUrl = .Cells(i, 4).Value
If xUrl <> vbNullString Then
Do
DoEvents
On Error Resume Next
Set wb = Workbooks.Open(xUrl, Format:=6, DELIMITER:=",") 'add other tests for valid url?
On Error GoTo 0
If Not wb Is Nothing Then 'remember to save and exit do
wb.SaveAs wbMain.Path & "\" & wb.Name, AccessMode:=xlExclusive, ConflictResolution:=Excel.XlSaveConflictResolution.xlLocalSessionChanges 'Credit to @Sorceri https://stackoverflow.com/a/14634781/6241235
wb.Close True
Exit Do
Else
Application.Wait Now + TimeSerial(0, 0, 5)
End If
Loop While numberOfTries < MAX_RETRIES
End If
End With
ws.Cells(i, 6) = IIf(wb Is Nothing, "FAIL", "OK")
Set wb = Nothing
Next
End If
ENDING
End Sub
Option Explicit“使用Option Explicit
公共子检索yahoodata()
Const MAX_重试次数(只要=3次)
Dim i为长,ws为工作表,lastRow为长“使用长
将wbMain作为工作簿、wb作为工作簿、xUrl作为字符串“声明xUrl”
Dim xtable作为字符串“临时分配”。
开始“这些是什么潜艇?”?
Set wbMain=Workbooks(“SHARE PRICE CREATOR.xlsb”)''放入一个变量。这是开放的。
设置ws=wbMain.Worksheets(“链接”)
lastRow=ws.Cells(ws.Rows.Count,“D”).End(xlUp)。Row'我想您想从第2行开始计数吗
如果lastRow>=2,则
对于i=2到最后一行
如果i Mod 100=0,则Application.Wait Now+TimeSerial(0,0,5)’每100个请求都有一个暂停
次数=0
与ws
xtable=.Cells(i,5).Value'?什么是xtable及其数据类型_
声明它并在代码顶部使用Option Explicit_
还有,它将在哪里使用?
xUrl=.Cells(i,4).Value
如果是xUrl vbNullString,则
做
多芬特
出错时继续下一步
设置wb=Workbooks.Open(xUrl,格式:=6,分隔符:=“,”)是否为有效url添加其他测试?
错误转到0
如果不是wb,则“记住保存并退出”
wb.SaveAs wbMain.Path&“\”&wb.Name,访问模式:=xlExclusive,conflictdesolution:=Excel.xlsaveconflictdesolution.xlLocalSessionChanges的贷方为@Sorcerihttps://stackoverflow.com/a/14634781/6241235
wb.Close为真
退出Do
其他的
应用程序。立即等待+时间序列(0,0,5)
如果结束
循环次数<最大重试次数
如果结束
以
ws.Cells(i,6)=IIf(wb为零,“FAIL”,“OK”)
设置wb=Nothing
下一个
如果结束
结束
端接头
旁注:我不认为下一步的错误恢复对您的错误处理有任何作用。将工作簿(“SHARE PRICE CREATOR.xlsb”).工作表(“links”)放入一个变量中,并用它限定您的范围调用。避免隐式的Activesheet引用。使用Err.Clear清除错误在运行到错误处理程序之前,如果所有任务都已完成,则需要一个退出子项才能成功完成。另外,您需要一个退出策略来避免无限循环的可能性。我个人会在进入下一个url之前使用最大重试次数策略,并且每x个请求都会等待一次,以成为一个好的网民。每20个周期等待一次是选项3,我不知道如何做到这一点,尽管我不知道如何用不同的范围复制代码