+ Reply to Thread
Results 1 to 9 of 9

VBA code needed / stock price macro ?

Hybrid View

  1. #1
    Registered User
    Join Date
    07-09-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    3

    VBA code needed / stock price macro ?

    hi everyone, can someone please help me with this one function i need to finish up a school project on historical stock prices in excel?? basically i need a way to perform this function:

    in row 1, i have 30 RANDOM historical dates, starting in B1 and going right

    in column A, i have 10 stock symbols, starting in A2 and going down

    Is there a way to automatically grab the ADJUSTED CLOSING PRICE for each symbol for each corresponding date?? if not i have to do it manually!!

    Your help in this matter is much appreciated!!!!
    Last edited by toekneetn; 07-09-2012 at 01:13 PM.

  2. #2
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    16,052

    Re: desperately need some vba help!!

    Your post does not comply with Rule 1 of our Forum RULES. Your post title should accurately and concisely describe your problem, not your anticipated solution. Use terms appropriate to a Google search. Poor thread titles, like Please Help, Urgent, Need Help, Formula Problem, Code Problem, and Need Advice will be addressed according to the OP's experience in the forum: If you have less than 10 posts, expect (and respond to) a request to change your thread title. If you have 10 or more posts, expect your post to be locked, so you can start a new thread with an appropriate title.
    To change a Title on your post, click EDIT then Go Advanced and change your title, if 2 days have passed ask a moderator to do it for you.

    Never use Merged Cells in Excel

  3. #3
    Registered User
    Join Date
    07-09-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: VBA code needed / stock price macro ?

    thx, i changed the title...now does anyone know how to do this? is there a code?

  4. #4
    Registered User
    Join Date
    07-09-2012
    Location
    Pittsburgh, PA USA
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: VBA code needed / stock price macro ?

    1. Download the historical prices for each stock from Yahoo.

    2. Embed them in a spreadsheet.

    3. Use HLookup/VLookup to find the date required.

  5. #5
    Registered User
    Join Date
    07-09-2012
    Location
    New York
    MS-Off Ver
    Excel 2007
    Posts
    3

    Re: VBA code needed / stock price macro ?

    thx for ur response joe...is there a way to just enter the symbol in excel and automatically grab the info from yahoo finance or whatever website?

  6. #6
    Registered User
    Join Date
    07-09-2012
    Location
    Pittsburgh, PA USA
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: VBA code needed / stock price macro ?

    Not easily. You'll need to write the code, or find an example online that you can modify to suit your purposes.

  7. #7
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: VBA code needed / stock price macro ?

    Like this:
    Public Function GetClose(ticker As String, closeDate As Date) As Double
    Dim results
    Dim m0nth As String
    If month(closeDate) < 10 Then m0nth = "0" & month(closeDate) - 1 Else m0nth = month(closeDate) - 1
    
    With CreateObject("msxml2.xmlhttp")
        .Open "Get", "http://finance.yahoo.com/q/hp?s=" & ticker & _
                            "&a=" & m0nth & _
                            "&b=" & Day(closeDate) & _
                            "&c=" & Year(closeDate) & _
                            "&d=" & m0nth & _
                            "&e=" & Day(closeDate) & _
                            "&f=" & Year(closeDate) & "&g=d"
        .send
        Do: DoEvents: Loop Until .readyState = 4
        results = Split(.responseText, "<td class=""yfnc_tabledata1"" align=""right"">")
        .abort
        GetClose = Val(results(UBound(results)))
    End With
    End Function

  8. #8
    Registered User
    Join Date
    07-09-2012
    Location
    Pittsburgh, PA USA
    MS-Off Ver
    Excel 2007
    Posts
    13

    Re: VBA code needed / stock price macro ?

    You've also solved my problem with code that crashes Excel mysteriously.

    Different code does not crash Excel, but achieves same purpose = no more problem.

    Thank you!

  9. #9
    Forum Guru Kyle123's Avatar
    Join Date
    03-10-2010
    Location
    Leeds
    MS-Off Ver
    365 Win 11
    Posts
    7,239

    Re: VBA code needed / stock price macro ?


+ 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