+ Reply to Thread
Results 1 to 5 of 5

Automatically retrieve Mutual Fund Data

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    12-16-2011
    Location
    Portland, OR
    MS-Off Ver
    Excel 2016 Preview
    Posts
    105

    Automatically retrieve Mutual Fund Data

    Hello!

    The goal here is to create a macro that will retrieve mutual fund info. Not the real-time quote but data about the mutual fund. Here is the macro that I recorded. The challenge is in being able to set things up such that the user can input up to 10 ticker symbols and grab, specifically, the Max Front End Sales Fee.

    My macro hardcoded the ticker GEGAX.
    How can this be set up such that user can load tickers into range A5:A14?



    Sub grabMFData()
    '
    ' grabMFData Macro
    '
    ' Keyboard Shortcut: Ctrl+m
    '
        Range("B16").Select
        ActiveWorkbook.Worksheets.Add
        With ActiveSheet.QueryTables.Add(Connection:= _
            "URL;http://finance.yahoo.com/q/pr?s=gegax+profile", Destination:=Range( _
            "$A$1"))
            .Name = "pr?s=rymgx+profile"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlSpecifiedTables
            .WebFormatting = xlWebFormattingNone
            .WebTables = "36"
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
        Sheets("Working Version").Select
        Range("A16").Select
    End Sub

    The code below wonderfully extract live quotes. And if the ticker symbol is bogus, this returns "???'" All good.
    I've been trying to modify this code to retrieve the Max Fee but it's not going so well.


    Sub LoopAllSymbols_RetrieveData()
    
    '   Local Variables
        Dim rngLookUpSym        As Range, rngQuerySym       As Range, rngQuerySymCo         As Range
            Dim rngQuerySymData As Range
        Dim qryTableStocks          As QueryTable
    
    '   Step 1 : Set Data Ranges
        Set rngLookUpSym = Worksheets("Stock Symbols").Range("A2")
        Set rngQuerySym = Worksheets("Web Query Page").Range("A1")
        Set rngQuerySymCo = Worksheets("Web Query Page").Range("A5")
        Set rngQuerySymData = Worksheets("Web Query Page").Range("A5").Range("D1:P1")
        Set qryTableStocks = ThisWorkbook.Worksheets("Web Query Page").QueryTables(1)
    
    '   Step 2 : Loop through list of stocks and retrieve market data
        Do While rngLookUpSym <> ""
            rngQuerySym = rngLookUpSym
        With qryTableStocks
            .Connection = _
            "URL;http://moneycentral.msn.com/investor/external/excel/quotes.asp?SYMBOL=" & rngQuerySym
            .WebSelectionType = xlEntirePage
            .WebFormatting = xlWebFormattingAll
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .Refresh BackgroundQuery:=False
        End With
            rngLookUpSym.Range("B1") = rngQuerySymCo
            rngLookUpSym.Range("D1:P1") = rngQuerySymData.Value
            Set rngLookUpSym = rngLookUpSym.Offset(1, 0)
        Loop
    
    End Sub
    
    
    
    Sub BetterWebQuery()
    '
    ' BetterWebQuery Macro
    ' Macro recorded 9/30/2004 by Atmel PC
    '
    
    '
        With ActiveSheet.QueryTables.Add(Connection:= _
            "FINDER;C:\Program Files\Microsoft Office\Office\Queries\Microsoft Investor Stock Quotes.iqy" _
            , Destination:=Range("A2"))
            .Name = "Microsoft Investor Stock Quotes_1"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = False
        
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlEntirePage
            .WebFormatting = xlWebFormattingAll
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .Refresh BackgroundQuery:=False
        End With
        Range("A1").Select
        ActiveCell.FormulaR1C1 = "Atml"
        Range("A1").Select
    End Sub
    Last edited by Habanero Time; 01-24-2012 at 09:05 PM.

  2. #2
    Registered User
    Join Date
    01-15-2010
    Location
    VA, USA
    MS-Off Ver
    Excel 2003
    Posts
    26

    Re: Automatically retrieve Mutual Fund Data

    Honestly a little confused because you had different querytables that pointed to different locations... I used the 1st because that was the only one where I could find a Max 12b fee.

    Sub LoopAllSymbols_RetrieveData()
    
    '   Local Variables
        Dim rngLookUpSym As Range, rngQuerySym As Range, rngQuerySymCo As Range
        Dim rngQuerySymData As Range
        Dim qryTableStocks As QueryTable
        Dim wsLookUp As Worksheet
    
    '   Step 1 : Set Data Ranges
        Set wsLookUp = Worksheets("Stock Symbols")
        Set rngLookUpSym = Worksheets("Stock Symbols").Range("A2")
        Set rngQuerySym = Worksheets("Web Query Page").Range("A1")
        Set rngQuerySymCo = Worksheets("Web Query Page").Range("B5")
        Set rngQuerySymData = Worksheets("Web Query Page").Range("A5").Range("D1:P1")
        Set qryTableStocks = Worksheets("Web Query Page").QueryTables(1)
    
    '   Step 2 : Loop through list of stocks and retrieve market data
        Do While rngLookUpSym <> ""
            rngQuerySym = rngLookUpSym
        With qryTableStocks
            .Connection = _
            "URL;http://finance.yahoo.com/q/pr?s=" & rngQuerySym & "+profile" & rngQuerySym
            .Name = "pr?s=rymgx+profile"
            .FieldNames = True
            .RowNumbers = False
            .FillAdjacentFormulas = False
            .PreserveFormatting = True
            .RefreshOnFileOpen = False
            .BackgroundQuery = True
            .RefreshStyle = xlInsertDeleteCells
            .SavePassword = False
            .SaveData = True
            .AdjustColumnWidth = True
            .RefreshPeriod = 0
            .WebSelectionType = xlSpecifiedTables
            .WebFormatting = xlWebFormattingNone
            .WebTables = "36"
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .WebDisableRedirections = False
            .Refresh BackgroundQuery:=False
        End With
            rngLookUpSym.Offset(0, 1) = rngQuerySymCo
            rngLookUpSym.Offset(0, 2) = rngQuerySymCo.Offset(0, 1)
            Set rngLookUpSym = rngLookUpSym.Offset(1, 0)
        Loop
    
    End Sub

  3. #3
    Forum Contributor
    Join Date
    12-16-2011
    Location
    Portland, OR
    MS-Off Ver
    Excel 2016 Preview
    Posts
    105

    Re: Automatically retrieve Mutual Fund Data

    vthokie, thanks for this, I'll try this when I get home.
    In response to your question about the code being confused: yes. I found some code and have been trying to massage it myself.

    I will follow up tonight.

  4. #4
    Forum Contributor
    Join Date
    12-16-2011
    Location
    Portland, OR
    MS-Off Ver
    Excel 2016 Preview
    Posts
    105

    Re: Automatically retrieve Mutual Fund Data

    vthokie, your code kinda worked. The code did go through and retrieved the table but not the correct cell.


    Here's code that I've massaged some more and it's retrieving the table, taking the contents of B7 from the Web Query page and placing it Column D on the Stock Page.

    Remaining issue: it's not handling errors well. How can this be adjusted in case a bogus mutual fund ticker is entered? Maybe the code would say "INVALID SYMBOL" instead of crashing.


    Sub LoopAllSymbols_RetrieveData2()
    
    '   Local Variables
        Dim rngLookUpSym        As Range, rngQuerySym       As Range, rngQuerySymCo         As Range
            Dim rngQuerySymData As Range
        Dim qryTableStocks          As QueryTable
    
    '   Step 1 : Set Data Ranges
        Set rngLookUpSym = Worksheets("Stock Symbols").Range("A2")
        Set rngQuerySym = Worksheets("Web Query Page").Range("A1")
        Set rngQuerySymCo = Worksheets("Web Query Page").Range("B7")
        Set rngQuerySymData = Worksheets("Web Query Page").Range("A5").Range("E1:P1")
        Set qryTableStocks = ThisWorkbook.Worksheets("Web Query Page").QueryTables(1)
    
    '   Step 2 : Loop through list of stocks and retrieve market data
        Do While rngLookUpSym <> ""
            rngQuerySym = rngLookUpSym
        With qryTableStocks
            .Connection = _
            "URL;http://finance.yahoo.com/q/pr?s=" & rngQuerySym & "+profile"
            '.WebSelectionType = xlEntirePage
             .WebSelectionType = xlSpecifiedTables
            .WebFormatting = xlWebFormattingAll
             .WebTables = "36"
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .Refresh BackgroundQuery:=False
        End With
            rngLookUpSym.Range("D1") = rngQuerySymCo
            rngLookUpSym.Range("E1:P1") = rngQuerySymData.Value
            Set rngLookUpSym = rngLookUpSym.Offset(1, 0)
        Loop
    
    End Sub

  5. #5
    Forum Contributor
    Join Date
    12-16-2011
    Location
    Portland, OR
    MS-Off Ver
    Excel 2016 Preview
    Posts
    105

    Re: Automatically retrieve Mutual Fund Data

    Still in need of help.

    In Steps 3 and 4

    The data on Web Query Page in range D5:I5 needs to go Stock Symbols in cells F:K on each row. Instead, it's bringing blank cells. I know this because I've had content in those cells and the code wiped out the content.

    The code is doing fine picking A5 on WQP and putting it in Column B on the Stock Symbols page.

    What's missing?


    '   Step 3 : Set Data Ranges for Real Time Quotes
        Set rngLookUpSym = Worksheets("Stock Symbols").Range("A2")
        Set rngQuerySym = Worksheets("Web Query Page").Range("A1")
        Set rngQuerySymCo = Worksheets("Web Query Page").Range("A5")
        Set rngQuerySymData = Worksheets("Web Query Page").Range("A5").Range("D5:I5")
        Set qryTableStocks = ThisWorkbook.Worksheets("Web Query Page").QueryTables(1)
    
    '   Step 4 : Loop through list of mutual fund symbols and retrieve data
        Do While rngLookUpSym <> ""
            rngQuerySym = rngLookUpSym
        With qryTableStocks
            .Connection = _
            "URL;http://moneycentral.msn.com/investor/external/excel/quotes.asp?SYMBOL=" & rngQuerySym
            .WebSelectionType = xlEntirePage
            .WebFormatting = xlWebFormattingAll
            .WebPreFormattedTextToColumns = True
            .WebConsecutiveDelimitersAsOne = True
            .WebSingleBlockTextImport = False
            .WebDisableDateRecognition = False
            .Refresh BackgroundQuery:=False
        End With
            rngLookUpSym.Range("B1") = rngQuerySymCo
            rngLookUpSym.Range("F1:K1") = rngQuerySymData.Value
            Set rngLookUpSym = rngLookUpSym.Offset(1, 0)
        Loop
    
    End Sub
    Last edited by Habanero Time; 01-19-2012 at 09:11 PM.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1