2013-08-09 2 views
0

vba 코드 액세스 yahoo 웹 사이트를 사용하여 주식 데이터를 가져 오는 것이 Excel을 만들었습니다.Yahoo 쿼리가 작동하지 않습니다.

엑셀은 대부분 잘 작동하지만 때로는 (규칙/동기 부여를 찾을 수 없다) 야후에서 데이터를 가져 오지 않습니다.

이상한 점은 디버거를 사용하여 단계별로 실행하면 작동하지만 매크로를 시작하면 작동하지 않으며 데이터를 가져올 수 없다는 것입니다.

의견이 있으십니까?

감사합니다,

지안 잠수정 아래

내가 사용 t ...

Sub StrongestSmallCaps() 
Dim frequency As String 
Dim numRows As Integer 
Dim LastRow As Integer 
Dim stockTicker As String 
Dim IndR As Integer 
Dim Simbolo As String 
Dim rsi As String 
Dim ShortInter As Boolean 
Dim NonIncr As Boolean 
Worksheets("GreenLine").Select 
LastRow = ActiveSheet.Cells(Rows.Count, "h").End(xlUp).Row 
frequency = "d" 



'Cancella contenuti celle stocastici 
Range("j2:k70").Clear 
Range("j2:k70").Select 
Selection.Style = "Stocastic" 

Range("i2:i70").Clear 
Range("i2:i70").Select 
Selection.Style = "Tick" 
Application.Wait Now + TimeValue("00:00:03") 
IndR = 2 
'Loop through all tickers 
For Ticker = 2 To LastRow 

    'Application.Wait Now + TimeValue("00:00:03") 
    stockTicker = Worksheets("GreenLine").Range("$h$" & Ticker) 

    If stockTicker = "" Then 
     GoTo NextIteration 
    End If 

    Sheets.Add After:=Sheets(Sheets.Count) 
    ActiveSheet.Name = stockTicker 

    Cells(1, 1) = "Stock Quotes for " & stockTicker 
    Call DownloadStockQuotes(stockTicker, Worksheets("GreenLine").Range("$b$500"), Worksheets("GreenLine").Range("$b$600"), "$a$2", frequency) 

    'Application.Wait Now + TimeValue("00:00:03") 
    Columns("a:a").TextToColumns Destination:=Range("a1"), DataType:=xlDelimited, _ 
           TextQualifier:=xlDoubleQuote, ConsecutiveDelimiter:=False, Tab:=True, _ 
           Semicolon:=False, Comma:=True, Space:=False, other:=False, FieldInfo:=Array(Array(1, 1), Array(2, 1), Array(3, 1), Array(4, 1), Array(5, 1), Array(6, 1), Array(7, 1)) 
    Sheets(stockTicker).Columns("A:G").ColumnWidth = 10 


    LastRow = Sheets(stockTicker).UsedRange.Row - 2 + Sheets(stockTicker).UsedRange.Rows.Count 
    If LastRow < 3 Then 
     Application.DisplayAlerts = False 
     Sheets(stockTicker).delete 
     GoTo NextIteration 
     Application.DisplayAlerts = True 
    End If 

    Rows("1:1").Select 
    Selection.delete Shift:=xlUp 
    Columns("B:B").Select 
    Selection.delete Shift:=xlToLeft 
    Columns("E:E").Select 
    Selection.delete Shift:=xlToLeft 
    Columns("E:E").Select 
    Selection.delete Shift:=xlToLeft 

    Rows("2:2").Select 
    Selection.INSERT Shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove 

    'CALCOLA STOCHASTIC 
    Worksheets("GreenLine").Select 
    Range("Cb100:Cm122").Select 
    Selection.Copy 
    Worksheets("GreenLine").Select 
    Sheets(stockTicker).Select 
    Range("e1").Select 
    ActiveSheet.Paste 


    If Cells(3, 8) < 20 Then 
     Worksheets("GreenLine").Select 

     Cells(IndR, 9) = stockTicker 
     Cells(IndR, 10) = "BUY" 
     Cells(IndR, 10).Select 
     Selection.Style = "Oversold" 
     Application.DisplayAlerts = False 
     Sheets(stockTicker).delete 
     Application.DisplayAlerts = True 

     'CALCOLA RSI 
     'Sheets(stockTicker).Select 

     'If Cells(3, 16) < 20 Then 
     '  rsi = Cells(3, 16) 
     '  Worksheets("GreenLine").Select 
     ' 
     '  Cells(IndR, 9) = stockTicker 
     '  Cells(IndR, 11) = "OVS" 
     '  Cells(IndR, 11).Select 
     '  Selection.Style = "Oversold" 
     '  Selection.Style = "Comma" 
     '  IndR = IndR + 1 

     '  Application.DisplayAlerts = False 
     '  Sheets(stockTicker).delete 
     '  Application.DisplayAlerts = True 
     'Else 
     '  IndR = IndR + 1 
     '  Application.DisplayAlerts = False 
     '  Sheets(stockTicker).delete 
     '  Application.DisplayAlerts = True 
     'End If 
    Else 
     Application.DisplayAlerts = False 
     Sheets(stockTicker).delete 
     Application.DisplayAlerts = True 
     'Sheets(stockTicker).Select 
     'If Cells(3, 16) < 20 Then 
     '  rsi = Cells(3, 16) 
     '  Worksheets("GreenLine").Select 
     ' 
     '  Cells(IndR, 9) = stockTicker 
     '  Cells(IndR, 11) = "OVS" 
     '  Cells(IndR, 11).Select 
     '  Selection.Style = "Oversold" 
     '  Selection.Style = "Comma" 
     ' 
     '  IndR = IndR + 1 
     '  Application.DisplayAlerts = False 
     '  Sheets(stockTicker).delete 
     ' Application.DisplayAlerts = True 
     'Else 
     '  Application.DisplayAlerts = False 
     '  Sheets(stockTicker).delete 
     '  Application.DisplayAlerts = True 
     'End If 
    End If 

NextIteration: 
Next Ticker 

ErrorHandler: 

Worksheets("GreenLine").Select 
Application.ScreenUpdating = True 


Range("h2:h70").Clear 
Range("h2:h70").Select 
Selection.Style = "Normal" 

E 

차 하위

Sub DownloadStockQuotes(ByVal stockTicker As String, ByVal startDate As Date, ByVal endDate As Date, ByVal DestinationCell As String, ByVal freq As String) 

Dim qurl As String 
Dim StartMonth, StartDay, StartYear, EndMonth, EndDay, EndYear As String 
StartMonth = Format(Month(Date) - 8, "00") 
StartDay = Format(Day(Date), "00") 
StartYear = Format(Year(Date), "00") 

EndMonth = Format(Month(Date) - 1, "00") 
EndDay = Format(Day(Date), "00") 
EndYear = Format(Year(Date), "00") 
Application.Wait Now + TimeValue("00:00:03") 
qurl = "URL;http://table.finance.yahoo.com/table.csv?s=" + stockTicker + "&a=" + StartMonth + "&b=" + StartDay + "&c=" + StartYear + "&d=" + EndMonth + "&e=" + EndDay + "&f=" + EndYear + "&g=" + freq + "&ignore=.csv" 
Application.Wait Now + TimeValue("00:00:03") 
On Error GoTo ErrorHandler: 
With ActiveSheet.QueryTables.Add(Connection:=qurl, Destination:=Range(DestinationCell)) 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    .BackgroundQuery = True 
    .RefreshStyle = xlInsertDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .WebSelectionType = xlSpecifiedTables 
    .WebFormatting = xlWebFormattingNone 
    .WebTables = "20" 
    .WebPreFormattedTextToColumns = True 
    .WebConsecutiveDelimitersAsOne = True 
    .WebSingleBlockTextImport = False 
    .WebDisableDateRecognition = False 
    .WebDisableRedirections = False 
    .Refresh BackgroundQuery:=False 
End With 
ErrorHandler: 

End Sub 
+0

정말 "디버거에서 작동"하고 "자체적으로 작동하지 않습니까?" 그렇다면 보안 문제 일 수 있습니까? –

답변

0

내가하면됩니다 내가이 문제를 데이터를 검색 먼저 명령을 실행하십시오. 오류 재개시 다음을 누르십시오.이 오류는 1004 개의 오류가 발생하지만 D는 발생하지 않습니다. ata가 내 오류 란에 표시됩니다.

그런 다음 데이터를 가져온 후에 실제로 데이터가 있는지 확인하고 그렇지 않은 경우 다시 쿼리를 실행합니다. 몇 가지 알려지지 않은 이유로 무작위로 실패하고 거의 항상 두 번째로 작동합니다.

하지만 오래전에 게시 된 이후로 문제를 이미 해결했기를 바랍니다.