Documenti di Didattica
Documenti di Professioni
Documenti di Cultura
Debug.Print strCSV
The most recent information is in Line 2.
Sample Data
Line 1: Date,Open,High,Low,Close,Volume,Adj Close
Line 2: 2015-07-03,59.86,60.30,59.22,60.30,72800,60.30
ExitHere:
Exit Function
HandleErr:
MsgBox "Error " & Err.Number & ": " & Err.Description
Resume ExitHere
Resume
End Function
Function GetYahooData(strTicker As String, Optional dtDate As Variant)
' Date is optional - if omitted, use today. If value is not a date, throw er
ror.
If IsMissing(dtDate) Then
dtDate = Date
Else
If Not (IsDate(dtDate)) Then
GetYahooData = 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 'Get past 7 days to avoid error due to Market closed
days
' Debug.Print dtDate
' See https://code.google.com/p/yahoo-finance-managed/w/list, and
' See https://code.google.com/p/yahoo-finance-managed/wiki/csvHistQuotesDown
load
' 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" ' G -> Frequency: Daily, Weekly, or Monthly (d|w|m). di
vidends: g=v
'Debug.Print strURL
Set http = CreateObject("MSXML2.XMLHTTP")
http.Open "GET", strURL, False
http.Send
strCSV = http.responseText
'
'
'
'
'
Debug.Print strCSV
The most recent information is in Line 2.
Sample Data
Line 1: Date,Open,High,Low,Close,Volume,Adj Close
Line 2: 2015-07-03,59.86,60.30,59.22,60.30,72800,60.30
strRows() = Split(strCSV, Chr(10)) ' split the CSV into rows
GetYahooData = strRows(1)
' return the data for the day
Set http = Nothing
End Function
Function IsLiveStockDataCell(R As Range) As Boolean
'
'
'
'
bResult = False
strFormula = Trim(R.Formula)
If Left(strFormula, 1) = "=" Then
strFormula = Trim(Mid(strFormula, 2))
bResult = InStr(1, strFormula, "Stock", vbTextCompare) > 0
End If
LiveStockDataCell = bResult
End Function
Function IsShortSale(R As Range) As Boolean
bResult = False
If R.Cells.CountLarge = 1 Then
Row = R.Cells.Row
Col = R.Cells.Column
BuyDate = Cells(Row, 4)
'Buy Date is in column D
SellDate = Cells(Row, 11) 'Sell Date is in column K
If Not IsEmpty(SellDate) Then
If IsEmpty(BuyDate) Or (SellDate < BuyDate) Then
bResult = True
End If
End If
' Debug.Print "BuyDate: ", BuyDate, " Sell: ", SellDate, " [ ", bResult,
" ]"
IsShortSale = bResult
End If
End Function