我有一個小的Excel VBA宏檢索出給定公司在某一天的股票價值。該公司通過傳遞他們的股票代碼(「AZO」代表AutoZone,「WMT」代表沃爾瑪等)來指定,日期是從相鄰單元格獲取的實際日期值。Excel的VBA宏將不再有效(有可能http.Send問題)
它一直致力於精美的2。5年,但本週它剛剛停止工作,儘管我沒有任何改變任何東西。宏應該返回一個值,現在它只是返回#VALUE !.當我通過代碼,一切正常,直到我到了。發送(),它只是停止(沒有錯誤信息或提示,以什麼地方出了錯,它只是完全停止,好像執行結束)。我嘗試添加一個On Error子句,但它沒有被擊中。注意:我認爲它沒有什麼區別,但最初我的http.Send沒有括號,但我看到很多帶有(「」)的例子,所以我添加了它,但似乎沒有效果。
我的VBA經驗是非常有限,所以我希望有人能夠指出我在正確的方向。
CODE:
Function StockQuote(strTicker As String, Optional dtDate As Variant)
' Date is optional - if omitted, use today. If value is not a date, throw error.
If IsMissing(dtDate) Then
dtDate = Date
Else
If Not (IsDate(dtDate)) Then
StockQuote = CVErr(xlErrNum)
End If
End If
Dim dtPrevDate As Date
Dim strURL As String, strCSV As String, strRows() As String, strColumns() As String
Dim dbClose As Double
dtPrevDate = dtDate - 7
' Compile the request URL with start date and end date
strURL = "http://ichart.finance.yahoo.com/table.csv?s=" & strTicker & _
"&a=" & Month(dtPrevDate) - 1 & _
"&b=" & Day(dtPrevDate) & _
"&c=" & Year(dtPrevDate) & _
"&d=" & Month(dtDate) - 1 & _
"&e=" & Day(dtDate) & _
"&f=" & Year(dtDate) & _
"&g=d&ignore=.csv"
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", strURL, False
http.Send ("")
strCSV = http.responseText
' The most recent information is in row 2, just below the table headings.
' The price close is the 5th entry
strRows() = Split(strCSV, Chr(10)) ' split the CSV into rows
strColumns = Split(strRows(1), ",") ' split the relevant row into columns. 1 means 2nd row, starting at index 0
dbClose = strColumns(4) ' 4 means: 5th position, starting at index 0
StockQuote = dbClose
Set http = Nothing
End Function
似乎'Yahoo'是剛到21世紀,現在使用加密'HTTP' - 'HTTPS'。試試'strURL =「https://ichart.finance.yahoo.com/table.csv?s=」&strTicker&_' ... –
字面上的一個字母修復了一切...數字。謝謝您的幫助! – jheaton3