+ Reply to Thread
Results 1 to 10 of 10

Macro to download monthly stock data and to refresh that data with the macro

  1. #1
    Registered User
    Join Date
    10-04-2010
    Location
    Tajiskitnxmas
    MS-Off Ver
    Excel 2003
    Posts
    6

    Smile Macro to download monthly stock data and to refresh that data with the macro

    Hi, am i glad to find this forum!

    I know only how to write functions in VBA, but not do complex VBA codes so here's a kind of problem i'm facing:

    I'm trying to create a code to collect MONTHLY closing price stock data from a certain time period X(march 2007 for example) which i can input, till the last month today (october 2010), but i'm trying to make it refresh itself so that every time i click the macro button, and suppose it's november 2010, so it could i think drop all the cells down and then paste the november 2010 monthly data above?

    Here are the problems i ran into:

    MSN money central seems to be giving me only the quotes from today (live quotes, but not a summary of all quotes from t-1, t-2, t-3 etc)

    Yahoo finance seems to give me for some stocks dividend dates which seem to screw up the data a little (but i can correct this part). Here's the problem i can't make it work automatically, i have to manually go to the web query, type the stock ticker in yahoo.finance.com and then get the data, i can't manage to get an automatic way in which i just type the stock name into the excel bracket and then get the data, nor can i automatically manipulate the months or dates for the download.


    If you can help me show how to do this for monthly closing prices, i can then do this for yearly and daily open/close/high/low prices etc, which can be really helpful for other people too i think.

    Here's my excel file attached
    Attached Files Attached Files
    Last edited by Frankdude; 10-04-2010 at 02:51 AM.

  2. #2
    Registered User
    Join Date
    10-04-2010
    Location
    Tajiskitnxmas
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Macro to download monthly stock data and to refresh that data with the macro

    Anybody? Maybe an easier question would be, how do you make a macro using an excel input of a stock name and a time frame to get data from some query like yahoo finance? because when i try to record the macro for the query process to go to yahoo.finance.com etc, the macro code goes like :

    Please Login or Register  to view this content.
    I'm trying to make this part :
    "URL;http://finance.yahoo.com/q/hp?s=T+Historical+Prices", Destination:=Range _
    ("$G$3"))
    the T in "url" be modifiable by a cell inside of excel. If i directly type in the cell number instead of T in the "url", it doesn't work, anybody? How do i make this macro work automatically in response to the cell name?
    Attached Files Attached Files

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Macro to download monthly stock data and to refresh that data with the macro

    Hi Frankdude,

    Have you heard of Excel Web Queries? They may do just what you want. Read about them here:
    http://articles.techrepublic.com.com...g=feed&subj=tr
    or search the net (I use Bing)... Bing the net for "Excel Web Queries"

    After you learn more about what Excel already does then perhaps you can request assistance.

  4. #4
    Registered User
    Join Date
    10-04-2010
    Location
    Tajiskitnxmas
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Macro to download monthly stock data and to refresh that data with the macro

    Quote Originally Posted by MarvinP View Post
    Hi Frankdude,

    Have you heard of Excel Web Queries? They may do just what you want. Read about them here:
    http://articles.techrepublic.com.com...g=feed&subj=tr
    or search the net (I use Bing)... Bing the net for "Excel Web Queries"

    After you learn more about what Excel already does then perhaps you can request assistance.
    Thanks for that article Marvin, it talks about exactly what i need, but i don't understand the parameters part of the VBA code which is exactly what i need to figure out. The author offers two solutions for post data and get data. My question is, is the data from yahoo.finance post or get data? I think it's get, so the author offers this solution as i understand:
    Please Login or Register  to view this content.
    So i change URL connection to :
    Please Login or Register  to view this content.
    but it comes up red. Please, all i need to figure out is how to make the "stocksymbol" part read as if it's an excel cell like cell "G2". Have been trying to do the stuff on the link you gave me for an hour now still can't figure how to make it work in the VBA thing.

  5. #5
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Macro to download monthly stock data and to refresh that data with the macro

    Hi Frankdude,
    The line (put a space before StockSymbol)
    Please Login or Register  to view this content.
    recieves an argument that is a stock symbol (like "MSFT" or "DELL") - you knew that.
    So in your code you do something like
    Please Login or Register  to view this content.
    In cell G2 you have "Dell" or "MSFT"

    The line of code
    Please Login or Register  to view this content.
    Adds MSFT onto the string of the URL so
    Please Login or Register  to view this content.
    is what shows up in your bowser on the URL line.

    I hope that makes sense.

  6. #6
    Registered User
    Join Date
    10-04-2010
    Location
    Tajiskitnxmas
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Macro to download monthly stock data and to refresh that data with the macro

    I understand that, thanks, but once i compile the code the data is not loading, i think i messed up somewhere, could you help?

    Please Login or Register  to view this content.

  7. #7
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,168

    Re: Macro to download monthly stock data and to refresh that data with the macro

    Frankdude,

    You were so close...

    See the attached which works. Put the symbol in A1 and click the button.

    I think you will be able to work through the Web Queries now.

    Good job.
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    10-04-2010
    Location
    Tajiskitnxmas
    MS-Off Ver
    Excel 2003
    Posts
    6

    Re: Macro to download monthly stock data and to refresh that data with the macro

    Thank you so much marvin!

  9. #9
    Registered User
    Join Date
    05-23-2011
    Location
    new zealand
    MS-Off Ver
    Excel 2007
    Posts
    5

    Smile Re: Macro to download monthly stock data and to refresh that data with the macro

    Hi marvin, may be u can help me here... I had a similar query and everything works the way u explain above, however i am trying to retrieve Income statement, balance sheet and cash flow data. Now my problem is : The data that it retrieves is Quarterly data and not annual data, although i have written a macro for Annual data . Below is the code :
    How do i solve this problem...plz help

    At the same time if i replace the stockSymbol variable with a ticker like WMT (Walmart) then it works fine...

    Public Sub GetHistoricalStockPrices1(ByVal StockSymbol As String)
    With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;http://finance.yahoo.com/q/is?s=" & StockSymbol & "+Income+Statement&annual", Destination _
    :=Range("$c$3"))
    .Name = "is?s=A+Income+Statement&annual"
    .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 = "10"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    End With
    With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;http://finance.yahoo.com/q/bs?s=" & StockSymbol & "+Balance+Sheet&annual", Destination:= _
    Range("$I$3"))
    .Name = "bs?s=B+Balance+Sheet&annual"
    .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 = "10"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    End With

    With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;http://finance.yahoo.com/q/cf?s=" & StockSymbol & "+Cash+Flow&annual", Destination:= _
    Range("$O$3"))
    .Name = "cf?s=C+Cash+Flow&annual"
    .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 = "10"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    End With

    End Sub
    Sub RunFromButtonPress1()
    GetHistoricalStockPrices1 (ActiveSheet.Range("a1"))
    End Sub

  10. #10
    Registered User
    Join Date
    05-23-2011
    Location
    new zealand
    MS-Off Ver
    Excel 2007
    Posts
    5

    Smile Re: Macro to download monthly stock data and to refresh that data with the macro

    i am trying to retrieve Income statement, balance sheet and cash flow data. Now my problem is : The data that it retrieves is Quarterly data and not annual data, although i have written a macro for Annual data . Below is the code :
    How do i solve this problem...plz help

    At the same time if i replace the stockSymbol variable with a ticker like WMT (Walmart) then it works fine...

    Public Sub GetHistoricalStockPrices1(ByVal StockSymbol As String)
    With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;http://finance.yahoo.com/q/is?s=" & StockSymbol & "+Income+Statement&annual", Destination _
    :=Range("$c$3"))
    .Name = "is?s=A+Income+Statement&annual"
    .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 = "10"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    End With
    With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;http://finance.yahoo.com/q/bs?s=" & StockSymbol & "+Balance+Sheet&annual", Destination:= _
    Range("$I$3"))
    .Name = "bs?s=B+Balance+Sheet&annual"
    .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 = "10"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    End With

    With ActiveSheet.QueryTables.Add(Connection:= _
    "URL;http://finance.yahoo.com/q/cf?s=" & StockSymbol & "+Cash+Flow&annual", Destination:= _
    Range("$O$3"))
    .Name = "cf?s=C+Cash+Flow&annual"
    .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 = "10"
    .WebPreFormattedTextToColumns = True
    .WebConsecutiveDelimitersAsOne = True
    .WebSingleBlockTextImport = False
    .WebDisableDateRecognition = False
    .WebDisableRedirections = False
    .Refresh BackgroundQuery:=False
    End With

    End Sub
    Sub RunFromButtonPress1()
    GetHistoricalStockPrices1 (ActiveSheet.Range("a1"))
    End Sub

+ 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