+ Reply to Thread
Results 1 to 15 of 15

Calculating the prior month given the current month

  1. #1
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Calculating the prior month given the current month

    Hello everyone. I am currently coding a project which requires information from a prior month. What I have is a message box which prompts the user to enter the date: dd-mmm-YYYY format. That value is then stored as a variable deontoed fDate. I would like to create a sub macro which takes fDate and"subtracts a month" and create a file which will be named after that, for instance if the current month is 30-jan-2010, then a file will be created and named 31-dec-2009. My line of thought was "fdate -1", but that returns an error. Please see the code below, specificaly Sub CreateFiles (). Any help is greatly appreciated.


    Please Login or Register  to view this content.
    Last edited by AnthonyWB; 04-15-2010 at 03:11 PM.

  2. #2
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Calculating the prior month given the current month

    One way to move the date back by one month with VBa

    Please Login or Register  to view this content.
    or
    Please Login or Register  to view this content.

    The above works for up to the 30th of any month except February.

    There are probably smarter ways, just can't think just now.

    Cheers

    What do you want to do if the date is 31/03/2010 ?
    Last edited by Marcol; 04-15-2010 at 01:23 PM.

  3. #3
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Calculating the prior month given the current month

    I cannot quite follow your line of thought. Should I do this in a "hidden sheet" and save the prior-month-calculation and use that?

  4. #4
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Calculating the prior month given the current month

    Sorry only a demo, I should have said so.

    No just change Range("A1") to your date variable fDate

    Therefore
    Please Login or Register  to view this content.
    Please read my comments in Post #2 again

    Do you want to use the last date in any month rather than just moving the date back by one month?

    Cheers

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Calculating the prior month given the current month

    Quote Originally Posted by Marcol View Post
    Please Login or Register  to view this content.

    Anthony, I noticed in your original example you wanted to go from the fDate manually entered (30-JAN-2010) to the end of the month prior (31-DEC-1999).

    For that, you would probably have to convert whatever fDate value was given back to the first day of the current month, then subtract 1 day. Then you would need to convert it BACK to a string...
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  6. #6
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Calculating the prior month given the current month

    I don't want to change fDate, as this variable is used elsewhere in the code and I need to preserve the original value that was input by the user. I believe what I need is an additional variable say pastDate (As String ?), and set

    pastDate = (fDate,-1), or somthing like this. The date format will be dd-mmm-YYYY, for instance 31-dec-2009.

  7. #7
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Calculating the prior month given the current month

    JBeaucaire ,

    Is it possible for me to set

    Please Login or Register  to view this content.
    where pastDate is declared as a string publicaly?

  8. #8
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Calculating the prior month given the current month

    By using the formula you provided, JBeaucaire I reieve an error. By using the Intermediate window to assist with debuggins I also notice that pastDate is set to the year prior. For instance if I enter 31-dec-2009 then pastDate = 12/30/2008. Furthermore I need pastDate to have the same format as fDate. Then it will work.

    Please Login or Register  to view this content.

  9. #9
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Calculating the prior month given the current month

    hi all,

    I'm too slow at posting, but since I've scribbled it up & just to throw another variation into the mix...
    (very similar to those given, but with the new example of "day zero" & DateAdd)

    Please Login or Register  to view this content.
    hth
    Rob
    Last edited by shg; 04-15-2010 at 02:53 PM.
    Rob Brockett
    Kiwi in the UK
    Always learning & the best way to learn is to experience...

  10. #10
    Forum Guru (RIP) Marcol's Avatar
    Join Date
    12-23-2009
    Location
    Fife, Scotland
    MS-Off Ver
    Excel '97 & 2003/7
    Posts
    7,216

    Re: Calculating the prior month given the current month

    To get the last date in the month previous to the month in fDate

    Please Login or Register  to view this content.

    Cheers

  11. #11
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Calculating the prior month given the current month

    We are almost there it is just a matter of formatting:

    Please Login or Register  to view this content.
    fDatePrevious is stored as MM/DD/YYYY. I need dd-mmm-yyyy.

  12. #12
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Calculating the prior month given the current month

    Here is what I have come up with. Both fDatePrevious and fPriorDate are declared publically.

    Please Login or Register  to view this content.
    where I am setting

    Please Login or Register  to view this content.
    and
    Please Login or Register  to view this content.
    The code now executes, but I when i check the value of fPriorDate in the intermediate window, its value is empty?

  13. #13
    Forum Contributor
    Join Date
    03-30-2010
    Location
    Washington DC
    MS-Off Ver
    Excel 2007
    Posts
    458

    Re: Calculating the prior month given the current month

    I got it, thank you very much everyone.

    Please Login or Register  to view this content.

  14. #14
    Forum Expert
    Join Date
    01-03-2006
    Location
    Waikato, New Zealand
    MS-Off Ver
    2010 @ work & 2007 @ home
    Posts
    2,243

    Re: Calculating the prior month given the current month

    Cool, I'm pleased we could help
    Thanks for marking the thread as Solved.


    Rob

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Calculating the prior month given the current month

    Cool, I was about to start another pot of coffee!

    Go Team.

+ 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