我已經建立了一個宏,旨在循環遍歷股票行情並在一個列中,並使用網路查詢從雅虎財經提取這些股票行情的數據
對於 10、15、20 個 Web 查詢,它運作良好,但在前 20 或 30 個 Web 查詢中的某個時刻,它毫無失敗地轟炸了 excel
我首先要說的是,我是 VBA 程式碼的極端業餘愛好者,但我嘗試了一些方法來解決這個問題(清除快取、使用暫停),但它們似乎不起作用。
它不會每次都在同一專案上失敗,但狀態欄中總是有「連接到網路」文本,所以我覺得它與連接超時有關,但我不確定此時如何攻擊它。歡迎任何想法,以及我可能缺少的任何程式碼優化..謝謝!
Sub GetData()
Application.Calculation = xlManual
' make the website a variable
Dim sURL As String
Dim Ticker As String
Dim iRow As Integer
Dim iCol As Integer
Dim wqError As ErrObject
' create web query if it doesn't exist
If Worksheets("query").QueryTables.Count = 0 Then
With Worksheets("query").QueryTables.Add(Connection:="URL;", Destination:=Range("Query!A1"))
.Name = "market_data.asp"
.FieldNames = True
.RowNumbers = False
.FillAdjacentFormulas = False
.PreserveFormatting = True
.RefreshOnFileOpen = False
.BackgroundQuery = False
.RefreshStyle = xlInsertDeleteCells
.SavePassword = False
.SaveData = True
.AdjustColumnWidth = True
.RefreshPeriod = 0
.WebSelectionType = xlSpecifiedTables
.WebFormatting = xlWebFormattingNone
.WebTables = "4"
.WebPreFormattedTextToColumns = True
.WebConsecutiveDelimitersAsOne = True
.WebSingleBlockTextImport = False
.WebDisableDateRecognition = False
.WebDisableRedirections = False
End With
End If
iRow = 2
iCol = 2
'Loop through Tickers
Do While Worksheets("Data").Range("A" & iRow).Value <> ""
Ticker = Worksheets("Data").Range("A" & iRow).Value
sURL = "http://finance.yahoo.com/q?s=" & Ticker
With Worksheets("query")
.Cells.Clear
.QueryTables(1).Connection = "URL;" & sURL
On Error Resume Next
.QueryTables(1).Refresh BackgroundQuery:=False
Set wqError = Err
On Error GoTo 0
If wqError.Number = 0 Then 'No error
.Range("B1").Copy Worksheets("Data").Cells(iRow, iCol)
.Range("B5").Copy Worksheets("Data").Cells(iRow, iCol + 1)
.Range("B13:B14").Copy Worksheets("Data").Cells(iRow, iCol + 2)
.Range("B18").Copy Worksheets("Data").Cells(iRow, iCol + 4)
.Range("B15").Copy Worksheets("Data").Cells(iRow, iCol + 5)
.Range("B22").Copy Worksheets("Data").Cells(iRow, iCol + 6)
.Range("B16").Copy Worksheets("Data").Cells(iRow, iCol + 7)
.Range("B20").Copy Worksheets("Data").Cells(iRow, iCol + 8)
.Range("B19").Copy Worksheets("Data").Cells(iRow, iCol + 9)
.Range("B25").Copy Worksheets("Data").Cells(iRow, iCol + 10)
.Range("B24").Copy Worksheets("Data").Cells(iRow, iCol + 11)
ElseIf wqError.Number <> 1004 Then
'Report error because it isn't the expected error 1004 Web query returned no data
MsgBox "Web query refresh for " & String(2, vbCrLf) & sURL & String(2, vbCrLf) & " returned error number " & wqError.Number & String(2, vbCrLf) & wqError.Description
End If
End With
iRow = iRow + 1
If iRow Mod 5 = 0 Then Delete_IE_Cache
If iRow Mod 20 = 0 Then ActiveWorkbook.Save
If iRow Mod 20 = 0 Then Application.Wait (Now + TimeValue("0:00:03"))
Loop
'Format results
With Sheets("data")
Range("A:M").HorizontalAlignment = xlCenter
Range("A:A").NumberFormat = "Text"
Range("D:D").NumberFormat = "Text"
Range("I:I").NumberFormat = "Text"
Range("B:C").NumberFormat = "0.00"
Range("E:H").NumberFormat = "0.00"
Range("K:M").NumberFormat = "0.00"
End With
Application.Calculation = xlCalculationAutomatic
End Sub
答案1
我沒有看到任何等待頁面完成加載的程式碼...可能沒有必要,但是將其放在模組的頂部,然後在導航到網站+stockticker 後調用它也沒有什麼壞處。
Private Declare Sub AppSleep Lib "kernel32" Alias "Sleep" (ByVal dwMilliseconds As Long)
Public Sub PauseApp(PauseInSeconds As Long)
Call AppSleep(PauseInSeconds)
End Sub
然後在你的程式碼中,
sURL = "http://finance.yahoo.com/q?s=" & Ticker
Call sleepie(sURL)
就像我說的,它可能根本無法解決您的問題,但絕對會有所幫助。