0

I currently have a spreadsheet that has the dates (excluding weekends and holidays) for the past 10 years in column A. For example, A1 contains "7/19/2007" and A2520 contains "7/19/2017"

Column E contains the closing price for the stock SPY on those corresponding dates.

I am trying to figure out the standard deviation for the past 5 years. In order to do so, my idea was to write a VBA code that would select today's date and the previous five years, and then use that to calculate the standard deviation.

This list is updated everyday, meaning tomorrow, it will contain 7/20/2017 and the closing price for that day. My issue is that I cannot figure out how to make it so it will select today's date and the past five years, so then I can calculate the standard deviation.

Thank you guys for all your help! Sorry if this seems simple, I have just started learning VBA last week!

elguapo
  • 155
  • 1
  • 14
  • How do you want to select it? A range? Can you use `SumIfs()` or a filter perhaps? What have you tried? – BruceWayne Jul 19 '17 at 23:49
  • Hey! So the way I'm thinking is for it to select the range of cells starting with "today's date" and for it to go back 5 years of trading days (appx 1260 days). I can't use SumIfs, at least not to my knowledge, because I'm not trying to add them all up. And I'm not sure what you mean by filter, i don't really know how to incorporate that into the VBA code. Sorry if I am not being clear I'm new to this, I can clear up any more questions. Thanks! – elguapo Jul 20 '17 at 01:34
  • Hmm, I think i wasn't clear in my last comment, my goal is to be able to select these cells, then offset it so I can select the column where the prices are. There is probably a more efficient way, but I'm not sure what else I can do. My goal after selecting this is to have each cell divide by the previous one and then subtract one. For example, if E1 was 100 and E2 was 110, it would be (110/100)-1 – elguapo Jul 20 '17 at 01:44

1 Answers1

0

How's this? I make a few assumptions, like your dates are contiguous, and there's no empty cell in your Date column. I also assume your dates are in order, ascending. (So your day 10 years ago is in say row 10, and today is in row 1000).

Sub get_difference()
Dim dateRng As Range, cel As Range, priceRng As Range
Dim dateCol As Long, stockCol As Long, lastDate As Range
Dim tdyDate As Date, decadeAgo As Date

dateCol = 1 ' column A has your dates
stockCol = 5

tdyDate = WorksheetFunction.Text(Now(), "mm/dd/yyyy")
decadeAgo = get_Previous_Date(tdyDate)
Debug.Print decadeAgo

With Sheets("Stock Prices") ' change name as necessary
    With .Columns(dateCol)
        Set lastDate = .Find(what:=tdyDate) ' Assuming no break in data from A1
        'lastDate.Select
        Set cel = .Find(what:=decadeAgo)
        'cel.Select
    End With
    Set rng = .Range(.Cells(cel.Row, dateCol), .Cells(lastDate.Row, dateCol))
    'rng.Select
    Set priceRng = rng.Offset(0, stockCol - dateCol)
    'priceRng.Select
    'priceRng.Offset(0, 1).Select
    priceRng.Offset(0, 1).FormulaR1C1 = "=IFERROR((RC[-1]/R[-1]C[-1])-1,"""")"
End With

End Sub
Function get_Previous_Date(Dt As Date) As Date
    ' https://www.mrexcel.com/forum/excel-questions/37667-how-subtract-year-date-2.html
    Dim numYearsBefore as Long, numDaysBefore as Long, numMonthsBefore as Long
    numYearsBefore = 10 ' Change this to any amount of years
    numDaysBefore = 0
    numMonthsBefore = 0
    get_Previous_Date = DateSerial(Year(Dt) - numYearsBefore, Month(Dt) - numMonthsBefore, Day(Dt) - numDaysBefore)
End Function

Make changes as needed, i.e. sheet name (I called mine "Stock Prices"), and the columns. It's also a little verbose, and could be made more compact, but I figured it'd help you learn to keep it like that. I suggest stepping through with F8 to see what happens, and uncommenting the .select lines so you can visually see what it's doing.

BruceWayne
  • 22,923
  • 15
  • 65
  • 110
  • Hey! Wow that is amazing, thanks for listing out the steps, it really helps me understand it much more. I'm still sort of working out how to then figure out the standard deviation, but I'm sure it'll be much easier from here. One question I have is I noticed you used decadeAgo, is there anyway I could make it 5 years ago? Instead of 10. I wasn't able to find what VBA uses to refer to 5 years ago. Thanks again for your help! – elguapo Jul 20 '17 at 05:20
  • @vba.Beginner - Of course! THe part that was making it 10 years ago is in the `Function PY()...`. See my edit, hopefully it's more clear! Again, I suggest stepping through the macro with `F8`, as that makes it go line by line, and uncomment out the `.Select`, so you can see in Excel what's happening a little easier. Glad it helped! – BruceWayne Jul 20 '17 at 05:32
  • @vba.Beginner - If you wanted to do it, say, 5 years and 3 days before, you'd just do `get_Previous_Date = DateSerial(Year(Dt) - 5, Month(Dt), Day(Dt) - 3)`. If you wanted just two months, `get_Previous_Date = DateSerial(Year(Dt), Month(Dt) - 2, Day(Dt))`. Does that make sense? [note: i've updated that function name to be a little more intuitive] – BruceWayne Jul 20 '17 at 05:35
  • @vba.Beginner - And finally, I'm almost positive there's a native `Standard Deviation` function in Excel. Just look in to that and you should be good to go! [See here](http://researchbasics.education.uconn.edu/calculatingmeanstandarddev/) for one example, and [here for another](https://support.office.com/en-us/article/STDEV-function-51fecaaa-231e-4bbb-9230-33650a72c9b0) – BruceWayne Jul 20 '17 at 05:37
  • Sorry for the late reply, I've just gotten access to my computer. Yes! That makes a lot more sense now that you've explained it, thank you so much! I am going to play around with the Standard Deviation function, then I'm sure I will be able to figure it out from there. Another more off-topic question, how did you get so good at VBA? Is there a specific website or anything you went to learn? I have been taking the WiseOwl tutorials on youtube, but that only goes so far. Thanks again for all your help! – elguapo Jul 20 '17 at 16:28
  • @vba.Beginner - 1) Start with the macro recorder. That will help you learn some basics. 2) Learn how to avoid using [`.Select`/`.Activate`](https://stackoverflow.com/questions/10714251/), 3) Try as best you can yourself to Google things, and make attempts. Stepping through macros with `F8` helps too, since you can go line by line. 4) Of course, StackOverflow :D ...also there's [Chip Pearson's](http://www.cpearson.com/Excel/Topic.aspx) site (perhaps intimidating, but really really good and well written), and again, brush up on your Google-fu and Google away! – BruceWayne Jul 20 '17 at 16:33
  • Thanks for the advice BruceWayne! You've helped me way more than you think! :D :D :D – elguapo Jul 20 '17 at 17:22