View Single Post
  #9   Report Post  
Posted to microsoft.public.excel.programming
Matthew Herbert[_3_] Matthew Herbert[_3_] is offline
external usenet poster
 
Posts: 149
Default Import 1, 3, 6, and 12 month returns for a list of mutual fund

Ryan,

You can get the data yourself and do the calculations for any return for any
period. As I mentioned before, and as Ron pointed out, the XML object can be
used to query a server for information. Pricing data works equally as well
as the pre-canned pages that Yahoo! provides; however, the pricing data comes
in a nice delimited format rather than the HTML tags seen via the pre-canned
pages. See an example below.

Again, the results are printed to the Immediate Window (as were the results
from the web query I provided earlier). You can open the Immediate Window
via Ctrl+g or View|Immediate Window. The Immediate Window can also be
resized and moved. The Debug.Print statement prints items to the Immediate
Window.

Best,

Matt

Sub GetData()
Dim strRes As String
Dim varArr As Variant

'there are ways to build the URL to return specified data ranges to avoid
' having to wait for querying an entire stock history as opposed to
' the prices over the last year or two years
strRes = GetXMLHTTP("http://ichart.finance.yahoo.com/table.csv?s=INTC")
Debug.Print strRes

'split the results into an array
varArr = Split(strRes, vbLf)

'do something with the array, i.e. parse the data and loop through to
' perform your calculations

End Sub

Function GetXMLHTTP(strURL As String) As String

Dim objXMLHTTP As Object
Dim strText As String
Dim lngPos As Long

If strURL = "" Then
GetXMLHTTP = ""
Exit Function
End If

Set objXMLHTTP = CreateObject("Microsoft.XMLHTTP")

'should probably add an On Error statement to catch
' an instance when a bad strURL might throw an error
' in the .Open method.
With objXMLHTTP
.Open "GET", strURL, False
.Send
strText = .responseText
End With

If objXMLHTTP.statusText = "OK" Then
GetXMLHTTP = strText
Else
GetXMLHTTP = ""
End If

End Function

"ryguy7272" wrote:

Finally had a chance to revisit this; that macro is pretty sweet! Thanks so
much Ron!! SO, there's no way to get the 6-month returns because it's not on
the site, right. I looked, but didn't see it anywhere. I looked on
www.google.com/finance and didn't see any 6-month return metrics there
either. It seems like it's not a popular thing, I guess. Does anyone know?

Thanks again Ron!
--
Ryan---
If this information was helpful, please indicate this by clicking ''Yes''.


"ron" wrote:

Ryan...Give the following code a try. I didn't see 6-month returns,
but it should grab the rest of what you want...Ron


Dim fund_array(5) As String

Sub Fund_Returns()
fundarray = Array("RPBAX", "TRBCX", "PRWCX", "PRCOX", "PRDMX")

For i = 0 To 4
' get the source code
my_url = "http://finance.yahoo.com/q/pm?s=" & fundarray(i)
Set my_obj = CreateObject("MSXML2.XMLHTTP")
my_obj.Open "GET", my_url, False
my_obj.send
my_var = my_obj.responsetext
Set my_obj = Nothing

' determine the yields
pos_1 = InStr(1, my_var, "1-Month", vbTextCompare)
pos_2 = InStr(pos_1, my_var, "right", vbTextCompare)
pos_3 = InStr(pos_2, my_var, "", vbTextCompare)
pos_4 = InStr(pos_3, my_var, "<", vbTextCompare)
one_month_yld = Mid(my_var, 1 + pos_3, pos_4 - (1 + pos_3))

pos_1 = InStr(1, my_var, "3-Month", vbTextCompare)
pos_2 = InStr(pos_1, my_var, "right", vbTextCompare)
pos_3 = InStr(pos_2, my_var, "", vbTextCompare)
pos_4 = InStr(pos_3, my_var, "<", vbTextCompare)
three_month_yld = Mid(my_var, 1 + pos_3, pos_4 - (1 + pos_3))

pos_1 = InStr(1, my_var, "1-Year", vbTextCompare)
pos_11 = InStr(1 + pos_1, my_var, "1-Year", vbTextCompare)
pos_2 = InStr(pos_11, my_var, "right", vbTextCompare)
pos_3 = InStr(pos_2, my_var, "", vbTextCompare)
pos_4 = InStr(pos_3, my_var, "<", vbTextCompare)
one_year_yld = Mid(my_var, 1 + pos_3, pos_4 - (1 + pos_3))

' put the data where you want
Range("A" & 1 + i) = fundarray(i)
Range("B" & 1 + i) = one_month_yld
Range("C" & 1 + i) = three_month_yld
Range("D" & 1 + i) = one_year_yld
Next
End Sub


.