+ Reply to Thread
Results 1 to 9 of 9

Excel 2007 : Extracting the year from a string

  1. #1
    Registered User
    Join Date
    02-11-2010
    Location
    Maryland, USA
    MS-Off Ver
    Excel 2007
    Posts
    83

    Extracting the year from a string

    Hello everyone,

    I have a column that is in bad shape. It is full of alpha numeric text, including a date. (e.g. 63 Fed. Reg. 47319 (Sept. 4, 1998)). I can't count on the size of the cell always being the same, nor that the date is displayed as shown above in parentheses. Occasionally, the dates are displayed like this (3/9/95). I need to create a new column that contains the year. I fully expect that I may need to run a couple of different formulas to gather the 4 digit years and the 2 digit years. Any ideas on how to do this?

    Thanks in advance.

    Rob

  2. #2
    Valued Forum Contributor rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Extracting the year from a string

    It would help if you specify the range of the years (1990 -2010?)
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  3. #3
    Registered User
    Join Date
    02-11-2010
    Location
    Maryland, USA
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Extracting the year from a string

    Quote Originally Posted by rwgrietveld View Post
    It would help if you specify the range of the years (1990 -2010?)
    Sure. the dates listed will range from 1970-2009. I'd like to get them all, but would be happy to make this work for 1998-2008.

    Thanks again.

  4. #4
    Registered User
    Join Date
    02-11-2010
    Location
    Maryland, USA
    MS-Off Ver
    Excel 2007
    Posts
    83

    Re: Extracting the year from a string

    Does anyone have any suggestions on this?

  5. #5
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2003, 2010
    Posts
    40,678

    Re: Extracting the year from a string

    I suggest you post a workbook containing text strings that illustrate the gamut of formats you need to accommodate.
    Entia non sunt multiplicanda sine necessitate

  6. #6
    Registered User
    Join Date
    02-13-2010
    Location
    Copenhagen, Denmark
    MS-Off Ver
    Excel 2007
    Posts
    1

    Re: Extracting the year from a string

    Quote Originally Posted by shg View Post
    I suggest you post a workbook containing text strings that illustrate the gamut of formats you need to accommodate.
    I agree, that would definitely help.

    Here you have a sample of some code, but bear in mind that it has only been tested with the following data, and only while searching for years starting with "19xx"

    63 Fed. Reg. 47319 (Sept. 4, 2010)
    63 Fed. Reg. 47320 (Sept. 4, 1998)
    63 Fed. Reg. 47376 (Sept. 4, 1976)

    Please Login or Register  to view this content.
    It returns:

    63 Fed. Reg. 47319 (Sept. 4, 2010) Year not found
    63 Fed. Reg. 47320 (Sept. 4, 1998) 1998
    63 Fed. Reg. 47376 (Sept. 4, 1976) 1976


    Bear in mind that the code has a bug: If there is more than one place where "19" figures, then it will just take that one, and doesn't check if there's any other.
    The code fails, if you have data like 19 Fed. Reg. 47367 (Sept. 4, 1987), because the first time "19" appears the next two characters are not numeric.

    But I hope you get the idea of what can be done.

    Regards,

    TriciaMcMillan

  7. #7
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Extracting the year from a string

    If the dates as you say aren't always in parenthesis, aren't always the same no. of digits etc then perhaps making the one assumption that the year is always the last numeric value in the string perhaps:

    Please Login or Register  to view this content.

    So if say A1:A5 contain:

    63 Fed. Reg. 47319 (Sept. 4, 1998)
    63 Fed. Reg. 47319 (3/9/95)
    63 Fed. Reg. 47319 Sept. 4, 1998
    63 Fed. Reg. 47319 (3/9/04)
    63 Fed. Reg. 47319 Sept. 4, 2002
    Then with

    Please Login or Register  to view this content.

    B1:B5 would return 1998, 1995, 1998, 2004, 2002 respectively.

    If the above still falls foul of some of the string variations then you will definitely need to post a file with more detailed examples.
    Last edited by DonkeyOte; 02-14-2010 at 05:51 AM.

  8. #8
    Forum Guru DonkeyOte's Avatar
    Join Date
    10-22-2008
    Location
    Northumberland, UK
    MS-Off Ver
    O365
    Posts
    21,531

    Re: Extracting the year from a string

    I was just looking at this again to see if there would be a viable formula approach...

    If we make the same assumption that the year is the last numeric in the string and use the same example strings as prior post (A1:A5) then:

    Please Login or Register  to view this content.

    would return the associated year value...

    NOTE: the above would not work pre XL2007

    However unlike the UDF it would not adjust for 2 digit output - so you would need to account for that separately eg:

    Please Login or Register  to view this content.

    where B1 holds output of the earlier monstrosity.

    That said I'm sure the likes Ron C, daddylongs, shg etc could come up with something really clever ... (challenge)


    Again, all moot if year is not always the last numeric.
    Last edited by DonkeyOte; 02-14-2010 at 07:38 AM.

  9. #9
    Forum Expert martindwilson's Avatar
    Join Date
    06-23-2007
    Location
    London,England
    MS-Off Ver
    office 97 ,2007
    Posts
    19,320

    Re: Extracting the year from a string

    i think that shg has indicated the first starting point!
    "Unless otherwise stated all my comments are directed at OP"

    Mojito connoisseur and now happily retired
    where does code go ?
    look here
    how to insert code

    how to enter array formula

    why use -- in sumproduct
    recommended reading
    wiki Mojito

    how to say no convincingly

    most important thing you need
    Martin Wilson: SPV
    and RSMBC

+ 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