【发布时间】:2019-06-18 00:14:10
【问题描述】:
我有一个通过解析 JSON 来获取历史股票价格的代码。我需要在特定日期获得“收盘价”。我需要代码从 Excel 单元格中读取日期并粘贴与日期对应的价格。这是一个例子:
https://cloud.iexapis.com/stable/stock/AAPL/chart/1m?token=pk_98e61bb72fd84b7d8b5f19c579fd0d9d
以下是我的代码,但我需要对其进行修改,以便它可以循环查找所需的日期:
Sub getHistoricalData()
'Application.DisplayAlerts = False
Application.ScreenUpdating = False
Dim wb As Workbook
Dim ws As Worksheet
Dim rng As Range
Dim symbol As Variant
Dim n As Integer
Dim lastrow As Long
Dim myrequest As Variant
Dim i As Variant
Set wb = ActiveWorkbook
Set ws = Sheets("Sheet1")
ws.Activate
'Last row find
lastrow = ws.Cells(Rows.Count, "A").End(xlUp).Row
Set rng = ws.Range("A3:A" & lastrow)
'Clear Prior Prices
ws.Range("k3:k" & lastrow).ClearContents
n = 3
'Get Symbols list
For Each symbol In rng
Set myrequest = CreateObject("WinHttp.WinHttpRequest.5.1")
myrequest.Open "Get", "https://cloud.iexapis.com/stable/stock/" & symbol & "/chart/1m?token=pk_98e61bb72fd84b7d8b5f19c579fd0d9d" 'updated 06/15/2019
'Debug.Print myrequest.ResponseText
Dim Json As Object
Set Json = JsonConverter.ParseJson(myrequest.ResponseText)
'MsgBox (myrequest.ResponseText)
i = Json("Close")
ws.Range(Cells(n, 2), Cells(n, 2)) = i
n = n + 1
Next symbol
ws.Columns("k").AutoFit
'MsgBox ("Data is downloaded.")
ws.Range("k3:k" & lastrow).HorizontalAlignment = xlGeneral
ws.Range("k3:k" & lastrow).NumberFormat = "$#,##0.00"
Application.DisplayAlerts = True
Application.ScreenUpdating = False
End Sub
例如,我需要提取每个股票代码在 06/06/2019 的收盘价。
【问题讨论】: