2017-04-15 64 views
0

第一次在這裏發佈。我需要以下問題的幫助。Excel VBA:我如何提取定義日期範圍內的雅虎財務歷史數據

我從Excel VBA中的雅虎財經網站網址提取歷史價格。我得到了一個變量單元格,數據的開始日期和結束日期。我轉換了url以獲取這些單元格的值來獲取我的數據。

問題:該宏從「ticker」變量單元中提取數據,但它沒有給出我在「start/end date」變量單元中定義的日期範圍。相反,它給了我整個歷史數據。

原文鏈接

http://chart.finance.yahoo.com/table.csv?s=NVDA&a=3&b=15&c=2012&d=3&e=15&f=2017&g=m&ignore=.csv 

轉換鏈接

http://chart.finance.yahoo.com/table.csv?s= " & Tick1 & " &a= " & smonth & " &b= " & sday & " &c= " & syear & " &d= " & emonth & " &e= " & eday & " &f= " & eyear & " &g=m&ignore=.csv 

Sub book1() 

'Macro Sheet 1 

'Variables 

Dim Tick1 As String 
Dim Tick2 As String 
Dim Tick3 As String 

Dim sday As Long 
Dim smonth As Long 
Dim syear As Long 
Dim eday As Long 
Dim emonth As Long 
Dim eyear As Long 

Dim newsheet As Object 

'Delete content 
With Sheets("Sheet1") 
.Range("A12:D200").Clear 
End With 

'Variable cells 

Tick1 = Range("b1") 
Tick2 = Range("c1") 
Tick3 = Range("d1") 

sday = Day(Range("b2")) 
smonth = Month(Range("b2")) - 1 
syear = Year(Range("b2")) 

eday = Day(Range("b3")) 
emonth = Month(Range("b3")) - 1 
eyear = Year(Range("b3")) 

'Extract data 
'Ticker 1 

With ActiveSheet.QueryTables.Add(Connection:= _ 
    "TEXT;http://chart.finance.yahoo.com/table.csv?s= " & Tick1 & " &a= " & smonth & " &b= " & sday & " &c= " & syear & " &d= " & emonth & " &e= " & eday & " &f= " & eyear & " &g=m&ignore=.csv", _ 
    Destination:=Range("$A$12")) 
    .Name = Tick1 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    '.RefreshStyle = xlInsertDeleteCells 
    .RefreshStyle = xlOverwriteDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = 850 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = False 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = True 
    .TextFileSpaceDelimiter = False 
    .TextFileColumnDataTypes = Array(1, 9, 9, 9, 9, 9, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 

End With 

'Ticker 2 

With ActiveSheet.QueryTables.Add(Connection:= _ 
    "TEXT;http://chart.finance.yahoo.com/table.csv?s= " & Tick2 & " &a= " & smonth & " &b= " & sday & " &c= " & syear & " &d= " & emonth & " &e= " & eday & " &f= " & eyear & " &g=m&ignore=.csv", _ 
    Destination:=Range("$C$12")) 
    .Name = Tick2 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    '.RefreshStyle = xlInsertDeleteCells 
    .RefreshStyle = xlOverwriteDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = 850 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = False 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = True 
    .TextFileSpaceDelimiter = False 
    .TextFileColumnDataTypes = Array(9, 9, 9, 9, 9, 9, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 

End With 

'Ticker 3 

With ActiveSheet.QueryTables.Add(Connection:= _ 
    "TEXT;http://chart.finance.yahoo.com/table.csv?s= " & Tick3 & " &a= " & smonth & " &b= " & sday & " &c= " & syear & " &d= " & emonth & " &e= " & eday & " &f= " & eyear & " &g=m&ignore=.csv", _ 
    Destination:=Range("$D$12")) 
    .Name = Tick3 
    .FieldNames = True 
    .RowNumbers = False 
    .FillAdjacentFormulas = False 
    .PreserveFormatting = True 
    .RefreshOnFileOpen = False 
    '.RefreshStyle = xlInsertDeleteCells 
    .RefreshStyle = xlOverwriteDeleteCells 
    .SavePassword = False 
    .SaveData = True 
    .AdjustColumnWidth = True 
    .RefreshPeriod = 0 
    .TextFilePromptOnRefresh = False 
    .TextFilePlatform = 850 
    .TextFileStartRow = 1 
    .TextFileParseType = xlDelimited 
    .TextFileTextQualifier = xlTextQualifierDoubleQuote 
    .TextFileConsecutiveDelimiter = False 
    .TextFileTabDelimiter = False 
    .TextFileSemicolonDelimiter = False 
    .TextFileCommaDelimiter = True 
    .TextFileSpaceDelimiter = False 
    .TextFileColumnDataTypes = Array(9, 9, 9, 9, 9, 9, 1) 
    .TextFileTrailingMinusNumbers = True 
    .Refresh BackgroundQuery:=False 

With Sheets("Sheet1") 
.Rows(62 & ":" & .Rows.Count).Delete 
End With 

'Copy on newsheet 

Set newsheet = ThisWorkbook.Sheets.Add 
    newsheet.Name = "Copie" 

ThisWorkbook.Sheets("Sheet1").Range("A12:D62").Copy 
    ThisWorkbook.Sheets("Copie").Range("A1").Select 
     ThisWorkbook.Sheets("Copie").Paste 

End With 

End Sub 
+1

可能是您的B2和B3單元格格式。如果這些單元格是'General',並且您輸入的字符串是mm/dd/yyyy,它應該可以工作。另外,excel說'xlOverwriteDeleteCells'沒有被定義。 – Amorpheuses

+0

日期單元格格式設置爲日期mm/dd/yyyy,但仍不起作用。我也編輯了我的文章,包括整個程序,對此抱歉。 –

+0

爲什麼你在兩個URL中包含星號?刪除他們和任何空間呈現第一個鏈接罰款。 – Parfait

回答

0

你應該檢查什麼網址,您的宏的用途,使連接到雅虎財經。

你可以做到這一點隨着ActiveSheet.QueryTables.Add通過加入這一行權之前

MsgBox ("http://chart.finance.yahoo.com/table.csv?s= " & Tick1 & " &a= " & smonth & " &b= " & sday & " &c= " & syear & " &d= " & emonth & " &e= " & eday & " &f= " & eyear & " &g=m&ignore=.csv") 

當你看到你正在試圖打開實際的鏈接,你會發現你有很多的空間字符,你實際上不需要。 每個引號前後都有一個空白字符,並且不需要它們。 改變這一點,你的問題應該得到解決。

正確的路線應該是:

"TEXT;http://chart.finance.yahoo.com/table.csv?s=" & Tick1 & "&a=" & smonth & "&b=" & sday & "&c=" & syear & "&d=" & emonth & "&e=" & eday & "&f=" & eyear & "&g=m&ignore=.csv", _ 

另外,我不知道爲什麼你把你的開始/結束日期一個月減一?

smonth =月(範圍( 「B」)) - 1

如果您在一月使用日期將無法正常工作。

+0

非常感謝。您所說的空白字符會阻止應用日期範圍。關於這個月的減號,我的老師告訴我,Excel認爲1月份是0月份,所以他說要加-1,我並沒有真正讓自己變得誠實。但它有點似乎工作。非常感謝,我感謝你的幫助。 –