excel vba http request download data from yahoo finance

后端 未结 2 1382
既然无缘
既然无缘 2020-12-10 20:42

I am in the process of making a program I wrote using excel vba faster.

The program downloads stock market data from the asx.

I want to get data from 2 urls:

2条回答
  •  旧时难觅i
    2020-12-10 20:58

    Try this revised code

    Sub GetYahooFinanceTable()
        Dim sURL As String, sResult As String
        Dim oResult As Variant, oData As Variant, R As Long, C As Long
    
        sURL = "http://ichart.finance.yahoo.com/table.txt?s=bhp.ax"
        Debug.Print "URL: " & sURL
        sResult = GetHTTPResult(sURL)
        oResult = Split(sResult, vbLf)
        Debug.Print "Lines of result: " & UBound(oResult)
        For R = 0 To UBound(oResult)
            oData = Split(oResult(R), ",")
            For C = 0 To UBound(oData)
                ActiveSheet.Cells(R + 1, C + 1) = oData(C)
            Next
        Next
        Set oResult = Nothing
    End Sub
    
    Function GetHTTPResult(sURL As String) As String
        Dim XMLHTTP As Variant, sResult As String
    
        Set XMLHTTP = CreateObject("WinHttp.WinHttpRequest.5.1")
        XMLHTTP.Open "GET", sURL, False
        XMLHTTP.Send
        Debug.Print "Status: " & XMLHTTP.Status & " - " & XMLHTTP.StatusText
        sResult = XMLHTTP.ResponseText
        Debug.Print "Length of response: " & Len(sResult)
        Set XMLHTTP = Nothing
        GetHTTPResult = sResult
    End Function
    

    This will split up the data into Rows so the max text length are not reached in a cell. Also this have further split the data with commas into corresponding columns.

    enter image description here

提交回复
热议问题