我使用此代码从 Yahoo Finance 下载特定代码的 CSV 格式。出于某种原因,CSV 文件正在与股价历史中每天开始新行的同一行中下载。因此,我无法使用打开的文件作为 # 方法进行输入。有任何想法吗?提前感谢您的任何帮助或建议。
Public Sub DownloadAllHistorical(ticker As Variant, stdate As Date, endate As Date, freq As String, loc As Variant)
Dim a, b, c, d, e, f As String
a = "&a=" & Month(stdate) - 1
b = "&b=" & Day(stdate)
c = "&c=" & Year(stdate)
d = "&d=" & Month(endate) - 1
e = "&e=" & Day(endate)
f = "&f=" & Year(endate)
g = "&g=" & freq
Dim URL As String
URL = "http://ichart.finance.yahoo.com/table.csv?s=" & _
ticker & _
a & b & c & _
d & e & f & _
g & "&ignore=.csv"
Dim http As MSXML2.XMLHTTP
Set http = New MSXML2.XMLHTTP
http.Open "GET", URL, False
http.send
URL = http.responseBody
If http.Status = 200 Then
Set ostream = CreateObject("ADODB.Stream")
ostream.Open
ostream.Type = 1
ostream.Write http.responseBody
ostream.SaveToFile loc & ticker & ".csv", 2 ' 1 = no overwrite, 2 = overwrite
ostream.Close
End If
Set ostream = Nothing
Set http = Nothing
End Sub