Here's the code I currently have:
Sub Test()
Dim ws As Worksheet
Dim qt As QueryTable
Dim URL As String
Dim Symbol As String
Set mep = Worksheets ("Managed Equity Portfolios")
Set ws = Worksheets("Hidden Sheet 3")
Symbol = Symbol & mep.Range("B5").Value
URL = "https://www.google.com/finance?q=MUTF:" + Symbol
Set qt = ws.QueryTables.Add( _
Connection:="URL;" & URL, _
Destination:=ws.Range("A1"))
qt.Refresh
Dim URL1 As String
Dim qt1 As QueryTable
Dim Symbol1 As String
Symbol1 = Symbol1 & mep.Range("B6").Value
URL1 = "https://www.google.com/finance?q=MUTF:" + Symbol1
Set qt1 = ws.QueryTables.Add( _
Connection:="URL1;" & URL1, _
Destination:=ws.Range("J1"))
qt1.Refresh
End Sub
So currently in the link, the symbol for the stock information I am trying to pull is at the end of the URL, "JLVIX"
I have all of my symbols on a different worksheet, all in Column B.
I know about yahoo API, and am using it, but it won't work because I need the 5 year standard deviation , which yahoo doesn't provide.
I would like the Macro to be able to pull the symbol from column B, and generate a QueryTable with that symbol at the end of the URL. Is there a more efficient way of doing this than creating 10 different macros with 10 different QueryTables on different worksheets?
Thank you!
Edit: It seems like when I try to make multiple QueryTables on one worksheet, they just stack on top of each other :(
Add params to the Sub
so you can call it for different contexts inside a loop with all the worksheets / equity symbols.
If all you need is the 5 year standard deviation you can change the Sub
into a Function
that returns the value.
Function get5YearStd(symbol As String) As Double
Dim ws As Worksheet
Dim qt As QueryTable
Dim URL As String
Set ws = Worksheets("Hidden Sheet 3") 'Or any other sheet
URL = "https://www.google.com/finance?q=MUTF:" + symbol
Set qt = ws.QueryTables.Add( _
Connection:="URL;" & URL, _
Destination:=ws.Range("A1") _
)
With qt
.RefreshStyle = xlOverwriteCells 'So the queries are always overwritten
.BackgroundQuery = False 'It needs to wait before fetching the updated value
.Refresh
End With
get5YearStd = ws.Range("D46").Value 'Range where the 5yr std.dev is
End Function
Then have another sub that call this function inside a loop for all your symbols
Sub test()
Dim rng1 As Range
Dim rng2 As Range
Dim equities As Range
Dim ws As Worksheet
Dim stddev As Double
Set ws = Worksheets("Managed Equity Portfolios")
Set rng1 = ws.Range("B5:B9")
Set rng2 = ws.Range("B11:B12")
'Loop over each cell in the informed ranges and call the function to retrive the data
For Each rng In Union(rng1, rng2)
stddev = get5YearStd(rng.Value)
Next
Debug.Print stddev
'Clear up connections created
For Each cn In ActiveWorkbook.Connections
cn.Delete
Next
'Clear variables
Set ws = Nothing
Set rng1 = Nothing
Set rng2 = Nothing
End Sub
please try this. run in new workbook
Sub Test()
Dim URL As String
URL = "https://www.google.com/finance?q=MUTF:JLVIX"
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Dim tc As Integer
tc = ws.QueryTables.Count
If tc > 0 Then
Dim i As Integer
For i = tc To 1 Step -1 ' delete any tables that may be in the worksheet
ws.QueryTables(i).Deleteworksheet
Next i
End If
Dim qt1 As QueryTable
Set qt1 = ws.QueryTables.Add( _
Connection:="URL;" & URL, _
Destination:=ws.Range("A1"))
Dim qt2 As QueryTable
Set qt2 = ws.QueryTables.Add( _
Connection:="URL;" & URL, _
Destination:=ws.Range("H1"))
qt2.Refresh ' fill second one first, just to see what happens
qt2.ResultRange.Select ' this is just to highlight the range
Stop ' check worksheet now
qt1.Refresh
qt1.ResultRange.Select ' this is just to highlight the range
End Sub
crude example of using the same table for pulling multiple data sources. data would be processed after each update
Sub Test()
Dim URL As String
URL = "https://www.google.com/finance?q=MUTF:JLVIX"
Dim ws As Worksheet
Set ws = Worksheets("Sheet1")
Dim i As Integer
For i = 1 To ws.QueryTables.Count
ws.QueryTables(1).Delete
Next i
Dim qt As QueryTable
Set qt = ws.QueryTables.Add( _
Connection:="URL;" & URL, _
Destination:=ws.Range("A1"))
qt.Refresh
qt.ResultRange.Select ' this is just to highlight the range
Stop ' check worksheet now
qt.ResultRange.ClearContents
Stop ' check worksheet now
qt.Connection = "URL;https://www.google.com/finance?q=MUTF:IBM"
qt.Destination = ws.Range("G3") ' this does not move the range
Stop ' check worksheet now
qt.Refresh
qt.ResultRange.Select ' this is just to highlight the range
Stop ' process data here
qt.ResultRange.ClearContents
End Sub
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.