+ Reply to Thread
Results 1 to 10 of 10

Capture date with only month and year, disregard day

  1. #1
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Holland, MI
    MS-Off Ver
    Excel 2007
    Posts
    110

    Capture date with only month and year, disregard day

    Hello all, thanks in advance for the help.

    I'm trying to capture a date using VBA in a specific format while disregarding the day. For example...

    On the spreadsheet, I'll have the date 10/1/2015. I would like to capture that date in a variable as Oct-15. When I call it up later, I want it to still be Oct-15.

    I'm running into a peculiar issue... I capture the date using the same code in 2 different lines and then messagebox the value stored and I get the date in 2 different formats. Here is the code...

    Please Login or Register  to view this content.
    For SD1 the messagebox displays Oct-15
    For SD2 the messagebox displays 10/15/2015

    I only did that because I wasn't getting a consistent result. Can anyone help me with this?

    My ultimate goal is to capture and use all of the dates as only month and year. Completely ignoring the day portion of the date.

    Thanks!

  2. #2
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Capture date with only month and year, disregard day

    Did you declare your variables SD1 and SD2 ?
    My guess is that one is a string and the other is a date

    One trick I use sometimes is to store the date as a string equal to YYYYMM. You can then retransform it in a date, with the day = 1.
    See SD3 in the following example :

    Please Login or Register  to view this content.
    GC Excel

    If this post helps, then click the star icon (*) in the bottom left-hand corner of my post to Add reputation.

  3. #3
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Capture date with only month and year, disregard day

    Hi

    It depends on how you want to use the data.

    Dates are actually numbers in excel, so there is no such thing in excel as a date that has no associated day. You could do one of three things, depending on how you want to use the data:

    - treat every date as the first of the month (allows you to perform calculations on the whole date)
    - return the month and year part of the date separately (allows you to perform separate operations on the month and year)
    - create a string comprising only the month and year with no day (treats all dates in a particular month and year as identical)

    this macro illustrates the three approaches, assuming your date is in cell A1:



    Please Login or Register  to view this content.

  4. #4
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Holland, MI
    MS-Off Ver
    Excel 2007
    Posts
    110

    Re: Capture date with only month and year, disregard day

    Thanks for the reply. Actually, I do have both of them formatted as dates. That line of code looks like this...

    Please Login or Register  to view this content.
    In the example you gave, I'm guessing you can only get the date as numbers, no month abbreviation. Is that correct?

  5. #5
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Holland, MI
    MS-Off Ver
    Excel 2007
    Posts
    110

    Re: Capture date with only month and year, disregard day

    Hi NickyC,
    Option 3 works best for what I'm trying to do and I put it into my code and it seems to do what I'm looking for. Now to add a twist. If I want to take that date and then use it as the start of a list of consecutive months, how would I handle that?

    Thanks!

  6. #6
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Capture date with only month and year, disregard day

    Hello,

    When you declare like this
    Please Login or Register  to view this content.
    only SD2 is declared as a Date. SD0 and SD1 are variant.

    the proper way to declare your variables would be :
    Please Login or Register  to view this content.

  7. #7
    Forum Expert
    Join Date
    08-02-2013
    Location
    Québec
    MS-Off Ver
    Excel 2003, 2007, 2013
    Posts
    1,412

    Re: Capture date with only month and year, disregard day

    In the example you gave, I'm guessing you can only get the date as numbers, no month abbreviation. Is that correct?
    This is correct.

  8. #8
    Forum Expert
    Join Date
    06-09-2010
    Location
    Australia
    MS-Off Ver
    Excel 2013
    Posts
    1,714

    Re: Capture date with only month and year, disregard day

    Hi
    can you explain what you are trying to do with your code?
    for example, if you want to create a string that can be sorted in date order, you can create a "string" using the the year and number of the month in a way that can be sorted,


    so

    Please Login or Register  to view this content.
    would yield a string that could be sorted by month then year, or

    Please Login or Register  to view this content.
    would yield a string that could sort by year, then month
    Last edited by NickyC; 05-26-2015 at 05:01 AM.

  9. #9
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Holland, MI
    MS-Off Ver
    Excel 2007
    Posts
    110

    Re: Capture date with only month and year, disregard day

    NickyC,
    It's kinda hard to explain what I'm trying to do. I have a list of items that start on a specific date. I want to create a timeline at the right side of the list that starts at the earliest date from the list and then adds a month as you move to the next cell.

    Once that timeline is in place, then I want to go back to each item on the list, find it's place on the timeline and start pasting values on the timeline for the list items. Attached is a sample spreadsheet showing what I'm trying to do along with my code. Hopefully it makes some sense.

    I tried using the 3 methods that you gave and thought that changing to the 1st of each month was the best way to go. But, I tried using your code and it keeps giving me the 15th of the month instead of the 1st of the month. Perhaps it's due to regional date differences or something like that. Any help would be greatly appreciated.
    Attached Files Attached Files

  10. #10
    Forum Contributor
    Join Date
    08-16-2010
    Location
    Holland, MI
    MS-Off Ver
    Excel 2007
    Posts
    110

    Re: Capture date with only month and year, disregard day

    Thanks for all the help. I ended up capturing the month and year separately (the way NickyC suggested) and putting them in individual cells rather than trying to combine them into 1 cell. Then, when doing the comparisons I would compare for matches in both cells. That way simplified things and I was able to get the result I was looking for.

+ 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. How can i show date in this form (month,last date of that month, Year)?
    By vjharry in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 10-24-2014, 04:39 AM
  2. Need to convert Month/Date/Year to Year/Month/Date so excel will recognize
    By juliettelam in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-25-2014, 04:17 PM
  3. Replies: 3
    Last Post: 09-04-2013, 10:49 AM
  4. extract date info, disregard year
    By koticphreak in forum Excel General
    Replies: 2
    Last Post: 04-10-2009, 01:49 PM
  5. Capture date of every month
    By jamespang in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-31-2006, 05:02 AM

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