2017-07-30 4 views
1

にダウンロードすると、yahooファイナンスからの自動ダウンロードをサポートしないため、他の情報源をチェックして、www.alphavantage.coが自分の要求に合っているようです。しかし、データはExcelに届きません。そこにいる誰もすでにプログラムしたのですか?私が使っているテストリンクはhttps://www.alphavantage.co/query?function=TIME_SERIES_DAILY_ADJUSTED&symbol=MSFT&apikey=demo&datatype=csvです。これは、ブラウザで開くときにCSVファイルにデータをダウンロードしますが、データがExcelに到着することはありません。事前にwww.alphavantage.coから株価データをExcel

多くのおかげで、VBA内 月

+1

そのリンク特定のシンボルのための)、N、高、低、クローズ、およびボリューム私のために働く。 – jamheadart

+0

「Excelに到着する」とはどういう意味ですか?彼らはあなたがExcelで開くかもしれないCSVファイルを約束しているようです。だから、到着すべきことはcsvファイルです。 – Variatus

+0

私はそれをダウンロードするには、次のコードを使用しています: –

答えて

2

、以下の参考文献>参考資料をツールを選択しを選択:

  • Microsoftスクリプトコントロール1.0
  • Microsoftスクリプトの実行時に

    次の関数は、最新のデータ(ope

    Public Function GetLastCloseData(symbol As String) As Dictionary 
        Dim scriptControl As Object 
        Dim json As Object 
        Dim time_series As Object 
        Dim date_data As Object 
        Dim date_label As String 
        Dim date_offset As Integer 
    
        Set scriptControl = CreateObject("MSScriptControl.ScriptControl") 
        scriptControl.Language = "JScript" 
    
        'Retrieve historical price data in JSON format 
        With CreateObject("MSXML2.XMLHTTP") 
         .Open "GET", "https://www.alphavantage.co/query?function=TIME_SERIES_DAILY&symbol=" & symbol & "&apikey=" & API_KEY, False 
         .send 
         Set json = scriptControl.Eval("(" + .responseText + ")") 
         .abort 
        End With 
    
        'CallByName returns an error if it cannot find the requested selection. 
        'The code is written to skip over those errors. 
        On Error Resume Next 
    
        Set time_series = CallByName(json, "Time Series (Daily)", VbGet) 
    
        'If time series property was found... 
        If Not time_series Is Nothing Then 
         date_offset = 0 
         'Retrieve the most recent closing price by looking for todays date. If it's not found, 
         'iterate through the last week of dates, stopping whenever the most recent is found. 
         While date_data Is Nothing And date_offset < 8 
          date_label = Format(DateAdd("d", -date_offset, Date), "yyyy-mm-dd") 
          Set date_data = CallByName(time_series, date_label, VbGet) 
          date_offset = date_offset + 1 
         Wend 
        End If 
    
        If Not date_data Is Nothing Then 
         Set GetLastCloseData = New Dictionary 
         With GetLastCloseData 
          .Add "Open", CDbl(CallByName(date_data, "1. open", VbGet)) 
          .Add "High", CDbl(CallByName(date_data, "2. high", VbGet)) 
          .Add "Low", CDbl(CallByName(date_data, "3. low", VbGet)) 
          .Add "Close", CDbl(CallByName(date_data, "4. close", VbGet)) 
          .Add "Volume", CLng(CallByName(date_data, "5. volume", VbGet)) 
         End With 
        End If 
    
        'set error handling back to normal 
        On Error GoTo 0 
    
    End Function 
    

    次のサブ結果を使用する方法を示し:

    Public Sub GetStockData() 
        Dim daily_data As Dictionary 
    
        Set daily_data = GetLastCloseData("SPY") 
        Debug.Print daily_data("Open") 
        Debug.Print daily_data("High") 
        Debug.Print daily_data("Low") 
        Debug.Print daily_data("Close") 
        Debug.Print daily_data("Volume") 
    End Sub 
    

    出力:

    260 
    260.15 
    259.57 
    259.76 
    45033392 
    
+0

注:この機能では、アルファバンタージュAPIキーをモジュールの先頭に次のように定義する必要があります。 'Private Const API_KEY =" api_key_here "' また、API文字列をURL文字列に手動で追加することもできます。 – NYITGUY

関連する問題