+ Reply to Thread
Results 1 to 8 of 8

Excel VBA - getting previous and last DATE from TODAY

  1. #1
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Excel VBA - getting previous and last DATE from TODAY

    Hello,

    In Excel VBA, is there a way to get the beginning date and the last date for the previous month from TODAY()?

    i.e. If TODAY is Jan 3, 2014, then I would like to have the beginning date as Dec 1, 2013 and the end date as Dec 31, 2013.
    i.e. If TODAY is Mar 18, 2012, then I would like to have the beginning date as Feb 1, 2012 and the end date as Feb 29, 2012 (yes 29, not 28).

    I believe you guys have the picture now.

    So basically, I need to have the AI to know the beginning of the year (January 2014 - takes Dec 2013) and if it happens to be February, the 'end date' is either 28th or 29th depending on the year.

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

    Re: Excel VBA - getting previous and last DATE from TODAY

    Use this and add into vba:
    start previous month:
    Formula: copy to clipboard
    Please Login or Register  to view this content.

    end previous month
    Formula: copy to clipboard
    Please Login or Register  to view this content.

  3. #3
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Excel VBA - getting previous and last DATE from TODAY

    Do you really need VBA when you could keep these value in a couple of cells. i.e.

    Beginning of previous month:
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    Last day of previous month
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    If you really do want a VBA variable then either read the variable from cells like the above or use the VBA equivalent

    Please Login or Register  to view this content.
    Richard Buttrey

    RIP - d. 06/10/2022

    If any of the responses have helped then please consider rating them by clicking the small star icon below the post.

  4. #4
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Re: Excel VBA - getting previous and last DATE from TODAY

    Hello, Yes I do need it to be done in VBA, since everything is going to be run on VBA, and there's no "input" on excel.

    I tried to put the code, but it ask me that "sub or function not defined" error.

  5. #5
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Re: Excel VBA - getting previous and last DATE from TODAY

    By the way, I am familiar with EOMONTH, but didn't know that it'll take into consideration of the "year" as well...1st scenario that I've put on the 1st thread...that is , if it's Jan 2014, it'll know that beginning date is Dec 1, 2013 and end date is Dec 31, 2013

  6. #6
    Forum Moderator zbor's Avatar
    Join Date
    02-10-2009
    Location
    Croatia
    MS-Off Ver
    365 ProPlus
    Posts
    15,602

    Re: Excel VBA - getting previous and last DATE from TODAY

    As RB wrote, you need to use worksheetfunction

  7. #7
    Valued Forum Contributor
    Join Date
    04-13-2011
    Location
    Ottawa, Canada
    MS-Off Ver
    365
    Posts
    1,067

    Re: Excel VBA - getting previous and last DATE from TODAY

    Got it!

    Solved!

    Reps up to you both!

  8. #8
    Forum Moderator - RIP Richard Buttrey's Avatar
    Join Date
    01-14-2008
    Location
    Stockton Heath, Cheshire, UK
    MS-Off Ver
    Office 365, Excel for Windows 2010 & Excel for Mac
    Posts
    29,464

    Re: Excel VBA - getting previous and last DATE from TODAY

    Quote Originally Posted by dluhut View Post
    Hello, Yes I do need it to be done in VBA, since everything is going to be run on VBA, and there's no "input" on excel.

    I tried to put the code, but it ask me that "sub or function not defined" error.
    Hi,

    I wasn't suggesting that any 'input' was necessary, other than the very first creation of the two functions in two cells. Once the two functions have been created you could hide them away on a non visible sheet if you wish.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Replies: 10
    Last Post: 09-06-2023, 08:19 PM
  2. Today date and previous data
    By Kevalin O in forum Excel General
    Replies: 2
    Last Post: 08-16-2013, 07:04 AM
  3. [SOLVED] Excel 2007 : Convert Today's Date To The Previous Month & Year
    By The_Snook in forum Excel General
    Replies: 8
    Last Post: 04-26-2012, 06:37 AM
  4. Replies: 2
    Last Post: 02-12-2010, 06:18 PM
  5. 25 days previous to today?
    By MaQ in forum Excel General
    Replies: 5
    Last Post: 03-14-2006, 03:00 PM

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