繁体   English   中英

Excel VBA:如何在定义的日期范围内提取雅虎财经历史数据

[英]Excel VBA : How can I extract yahoo finance historical data in a defined date range

第一次在这里发帖。 我需要有关以下问题的帮助。

我正在从 Excel VBA 中的雅虎财经网站 url 中提取历史价格。 我得到了一个用于代码的可变单元格,数据的开始日期和结束日期。 我转换了 url 以获取这些单元格的值来获取我的数据。

问题:宏从“ticker”变量单元格中提取数据,但它没有给出我在“开始/结束日期”变量单元格中定义的日期范围。 相反,它给了我整个历史数据。

原文链接

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

您应该检查宏使用哪些URL来建立与Yahoo Finance的连接。

您可以通过在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 = Month(Range(“ b2”))-1

如果您使用1月的日期,它将无法正常工作。

你解决这个问题了吗? 我也有同样的问题。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM