+ Reply to Thread
Results 1 to 25 of 25

Extract month and year from a cell

  1. #1
    Registered User
    Join Date
    08-12-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    8

    Extract month and year from a cell

    Hi everyone,

    My 1st post here

    I am trying to extract a month into one column and a year into the next column. First, the month extraction attempt:

    Book Date Created Date Formula used Result received Result should be
    10/13/2016 9/27/2015 =TEXT(G2, "mm") 10/13/2016 10


    It fails to give me the expected result ("10") and I am not sure where things are going wrong. Both source and result cell are formatted as General.

    If I try to use =MONTH(G2) as another extraction option, then the result becomes #VALUE!. Stepping through the calculation steps shows:
    "The cell currently being evaluated contains a constant"


    Many thanks in advance for any assistance provided.


    Cheers,
    Wojtek

  2. #2
    Valued Forum Contributor
    Join Date
    06-21-2013
    Location
    Bangladesh
    MS-Off Ver
    Excel 2000, Excel XP, Excel 2002, Excel 2007, Excel 2010, Excel 2013
    Posts
    975

    Re: Extract month and year from a cell

    Please Login or Register  to view this content.
    This formula should show the result 10.

    Please replace the above formula with that and let me know what's the result now?
    Please Login or Register  to view this content.

  3. #3
    Administrator FDibbins's Avatar
    Join Date
    12-29-2011
    Location
    Duncansville, PA USA
    MS-Off Ver
    Excel 7/10/13/16/365 (PC ver 2310)
    Posts
    53,048

    Re: Extract month and year from a cell

    If I try to use =MONTH(G2) as another extraction option, then the result becomes #VALUE!.
    That tells me your date is actually text, so try this...

    =month(datevalue(G2))
    =year(datevalue(g2))
    Note that using TEXT or left/mid/right will produce a text answer, which may need to be converted for later use

    If that still does not work,


    upload a small (clean) sample workbook (not a pic) of what you are working with, and what your expected outcome would look like.
    1. Use code tags for VBA. [code] Your Code [/code] (or use the # button)
    2. If your question is resolved, mark it SOLVED using the thread tools
    3. Click on the star if you think someone helped you

    Regards
    Ford

  4. #4
    Registered User
    Join Date
    08-12-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    8

    Re: Extract month and year from a cell

    Hi,
    After typing
    =DATE(RIGHT(G2,4),LEFT(G2,2),MID(G2,4,2))

    the result is
    13/10/2016

  5. #5
    Registered User
    Join Date
    08-12-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    8

    Re: Extract month and year from a cell

    Hi,

    Typing
    =month(datevalue(G2))
    =year(datevalue(g2))

    results in
    #VALUE!


    I will upload the sample worksheet in a sec.

  6. #6
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Extract month and year from a cell

    To attach your workbook:

    Click on GO ADVANCED and then scroll down to Manage Attachments to open the upload window.
    Ali


    Enthusiastic self-taught user of MS Excel who's always learning!
    Don't forget to say "thank you" in your thread to anyone who has offered you help. It's a universal courtesy.
    You can reward them by clicking on * Add Reputation below their user name on the left, if you wish.

    NB:
    as a Moderator, I never accept friendship requests.
    Forum Rules (updated August 2023): please read them here.

  7. #7
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Extract month and year from a cell

    Are you going to upload the workbook? Instructions in post #6.

  8. #8
    Registered User
    Join Date
    08-12-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    8

    Re: Extract month and year from a cell

    Hi,

    All I am getting when trying to attach the file is a blank white field next to the paper clip. Are there pop ups on this forum?

  9. #9
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Extract month and year from a cell

    Please read the instructions I gave you. The paperclip icon does not work.

  10. #10
    Registered User
    Join Date
    08-12-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    8

    Re: Extract month and year from a cell

    Something is odd - tried Chrome and Firefox:
    Forum error.JPG

  11. #11
    Registered User
    Join Date
    08-12-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    8

    Re: Extract month and year from a cell

    OK thanks, file should be uploaded now.
    Attached Files Attached Files

  12. #12
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Extract month and year from a cell

    I think the problem is your machine's locale: as in the UK, in Australia the general format for dates is dd/mm/yyyy, but you have got the dates in US format mm/dd/yyyy in column A (and elsewhere). If you change the first date to 13/10/2016, see what happens in D2.
    Last edited by AliGW; 08-13-2016 at 03:39 AM.

  13. #13
    Registered User
    Join Date
    08-12-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    8

    Re: Extract month and year from a cell

    Hi Ali,

    Thank you. When I change the data around from 10/13/2016 to 13/10/2016 then the correct value is shown.

    Trouble is that the data source must be in US format and it looks like my Excel is set for the AU region (which it needs to be as well).

    So at some point the regional format conversion needs to be accounted for.

    Cheers,
    W


    A further example with results that shows the limited cells with the correct output:

    Book Date Created Date Formula used Result received
    10/13/2016 9/27/2015 =TEXT(G2, "mm") 10/13/2016
    11/01/2016 11/18/2015 =TEXT(G2, "mm") 01
    12/18/2016 2/23/2016 =TEXT(G2, "mm") 12/18/2016
    9/13/2016 2/29/2016 =TEXT(G2, "mm") 9/13/2016
    11/30/2016 3/01/2016 =TEXT(G2, "mm") 11/30/2016
    10/13/2016 3/16/2016 =TEXT(G2, "mm") 10/13/2016
    9/23/2016 4/12/2016 =TEXT(G2, "mm") 9/23/2016
    11/28/2016 4/21/2016 =TEXT(G2, "mm") 11/28/2016
    12/22/2016 4/21/2016 =TEXT(G2, "mm") 12/22/2016
    9/30/2016 5/04/2016 =TEXT(G2, "mm") 9/30/2016
    9/13/2016 6/09/2016 =TEXT(G2, "mm") 9/13/2016
    10/13/2016 6/10/2016 =TEXT(G2, "mm") 10/13/2016
    8/13/2016 6/10/2016 =TEXT(G2, "mm") 8/13/2016
    10/13/2016 6/10/2016 =TEXT(G2, "mm") 10/13/2016
    10/13/2016 6/10/2016 =TEXT(G2, "mm") 10/13/2016
    8/04/2016 7/12/2016 =TEXT(G2, "mm") 04
    8/31/2016 7/12/2016 =TEXT(G2, "mm") 8/31/2016
    8/04/2016 7/20/2016 =TEXT(G2, "mm") 04

  14. #14
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Extract month and year from a cell

    So at which point do you want the conversion to happen?

    We don't need any more examples, as we know what the 'problem' is.

  15. #15
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Extract month and year from a cell

    Have a look at this discussion, which might help: http://superuser.com/questions/40843...es-as-uk-dates

    And this might provide an answer: http://superuser.com/questions/73037...erns-in-formul

  16. #16
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Extract month and year from a cell

    Quote Originally Posted by Wojtek-AU View Post
    A further example with results that shows the limited cells with the correct output:

    Book Date Created Date Formula used Result received
    10/13/2016 9/27/2015 =TEXT(G2, "mm") 10/13/2016
    11/01/2016 11/18/2015 =TEXT(G2, "mm") 01
    But it is not correct, if your data is in US format, then the second row should return November, not January!

    Go to the Data tab on the excel ribbon.

    Select column A (the entire column)

    Select Text to Columns on the ribbon.

    Click Next twice

    Change the Date dropdown to MDY

    Click Finish.

    Repeat for Column B.

    Now you should have the correct results.

  17. #17
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Extract month and year from a cell

    Thanks, Jason - I have learnt something new today! How have I never noticed that feature before?

  18. #18
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Extract month and year from a cell

    I have no idea, Ali! I thought you would be far more observant :eek

    I noticed your post with the links after submitting mine, and thought that you were suggesting the same thing.

    Guess I need to have a look at them now to see what the alternatives are

  19. #19
    Forum Moderator AliGW's Avatar
    Join Date
    08-10-2013
    Location
    Retired in Ipswich, Suffolk, but grew up in Sawley, Derbyshire (both in England)
    MS-Off Ver
    MS 365 Subscription Insider Beta Channel v. 2504 (Windows 11 Home 24H2 64-bit)
    Posts
    90,658

    Re: Extract month and year from a cell

    Quote Originally Posted by jason.b75 View Post
    Guess I need to have a look at them now to see what the alternatives are
    Me, too, obviously!!!

  20. #20
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Extract month and year from a cell

    The text to columns method seems to be the most reliable.

    Some of the other methods appear to get confused when some of the dates are recognisable in both MDY and DMY formats, i.e. when the day is 12 or less.

    This particular thread could also be solved with a fairly simple formula,

    =IFERROR(DAY(A2),--LEFT(A2,FIND("/",A2)-1))

    However, even the slightest inconsistency in the source data, such as dates delimited with - instead of / will cause the formula to fail, where text to columns will still work correctly.

  21. #21
    Valued Forum Contributor
    Join Date
    06-21-2013
    Location
    Bangladesh
    MS-Off Ver
    Excel 2000, Excel XP, Excel 2002, Excel 2007, Excel 2010, Excel 2013
    Posts
    975

    Re: Extract month and year from a cell

    Quote Originally Posted by Wojtek-AU View Post
    OK thanks, file should be uploaded now.
    I have checked your sample. You are facing the problem with date formatting. On your sample only A3 is in date formats and all others are in General format. If you want to keep this then use the below formula.
    Please Login or Register  to view this content.

  22. #22
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Extract month and year from a cell

    Sanram, please try to avoid posting inaccurate information.

    Your observation on formatting and your formula are both incorrect.

    Dates formatted as general are still recognised as dates, if the general format was the problem then the dates would show as numbers. This is completely different to regional formatting, which is the actual cause of the problem.

    This is also why your formula is incorrect. When the dates are in the wrong regional format, in the case of this thread, some dates are recognised, but the month and day get swapped around incorrectly, which is why my example of a possible formula uses DAY instead of MONTH to return the correct month of the valid dates.

  23. #23
    Valued Forum Contributor
    Join Date
    06-21-2013
    Location
    Bangladesh
    MS-Off Ver
    Excel 2000, Excel XP, Excel 2002, Excel 2007, Excel 2010, Excel 2013
    Posts
    975

    Re: Extract month and year from a cell

    jason.b75, I was trying to say the same. But may be my language wasn't perfect to understand. And I noticed my fault on formula just now. It should be Day instead of Month. Sorry for that.
    Last edited by sanram; 08-13-2016 at 07:04 AM.

  24. #24
    Registered User
    Join Date
    08-12-2016
    Location
    Australia
    MS-Off Ver
    2013
    Posts
    8

    Re: Extract month and year from a cell

    Hi all,

    First of all, many thanks to everyone for trying to help me.

    Secondly, Ali, thank you for prompting me with Qs and for the guidance on attaching files etc. As a n00b on this forum, even the most basic of tasks seemed complex...thank you for the links but due to the amount of the info they contained I did not get around to read them all just yet.

    And then comes Jason. I can't believe the simplicity of your solution! You have no idea what my workarounds have been till now (let's just say I had been pasting data into notepad & then back again!).

    Is there a button I can click to show it as the preferred answer?

    Once again, many thanks to all of you. The forum is what it is as a result of such collaboration


    Cheers,
    Wojtek

  25. #25
    Forum Expert
    Join Date
    06-08-2012
    Location
    Left the forum!
    MS-Off Ver
    Left the forum!
    Posts
    5,189

    Re: Extract month and year from a cell

    Hi Wojtek,

    Thanks for the feedback!

    There is no option to flag an answer as preferred, however we do ask that you mark your thread as Solved. This can be done from the Thread Tools drop down, which you will find on the right side of the page just above post #1.

    If this is something that you do on a regular basis then you could try recording a macro while carrying out the steps I suggested, then next time you just need to run the macro and that will do the rest for you.

    One limitation of the macro recorder though, is that it repeats exactly the same actions, so if the dates might be in different columns next time then it will not work correctly.

    Going a step further, code can be written to allow for differing sheet layouts if we can identify a pattern in the sheets to enable the code to find the date columns.

    Another alternative would be to correct the data when it is imported from the external data source, but that is where things can start to get a bit more complicated.

+ 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. [SOLVED] Extract Year, Month, Day from date
    By stoicy in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 03-28-2016, 11:48 PM
  2. Extract Year, Month, Day from date
    By stoicy in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2016, 01:41 PM
  3. [SOLVED] Extract 2 corresponding fields by Date/Month/Year selection
    By suchetherrah in forum Excel Formulas & Functions
    Replies: 9
    Last Post: 12-01-2015, 08:31 AM
  4. [SOLVED] Extract Day, Month and Year from a text box (user input) date value
    By HeyInKy in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 09-01-2014, 10:40 PM
  5. [SOLVED] Formual to Extract month and year from sheet name
    By rizmomin in forum Excel Formulas & Functions
    Replies: 13
    Last Post: 07-26-2014, 10:49 PM
  6. [SOLVED] formatting dates to automatically extract Year and month
    By alexcrofut in forum Excel General
    Replies: 1
    Last Post: 01-23-2014, 03:09 PM
  7. [SOLVED] Extract Year or Month only from a cell with date and time
    By djaurit in forum Excel Formulas & Functions
    Replies: 1
    Last Post: 01-28-2013, 04:03 PM

Tags for this Thread

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