Find year in cell

AMAS

Active Member
Joined
Apr 11, 2010
Messages
472
Hi,

This formula needs a tweak and I can't seem to get it right:

Code:
=IF(FIND({90,91,92,93,94,95,96,97,98,99},A2),{1990,1991,1992,1993,1994,1995,1996,1997,1998,1999},"No year found")

It looks for the two digit year in a cell and returns its four digit year format. It only seems to work if the first year (e.g. 90) is the first option in the Find. If I put for example {89,90} then it gives me a #value error.

AMAS
 
Assume list of names/numbers in A1:A10.

To extract the numeric values in the string, you could use this in B1 filled down:

=LOOKUP(99^99,--("0"&MID(A1,MIN(SEARCH({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),ROW($1:$10000))))
Here's a tweaked version which should be a bit more efficient.

=LOOKUP(5000,--MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),{1,2,3,4,5}))
 
Upvote 0

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Here's a tweaked version which should be a bit more efficient.
=LOOKUP(5000,--MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),{1,2,3,4,5}))
Another version:
Code:
=LOOKUP(5000,--MID(A1&"X",ROW(INDIRECT("1:"&LEN(A1))),2))

(Sorry, this formula works for text with the 2-digits year only)
 
Last edited:
Upvote 0
Here's a tweaked version which should be a bit more efficient.

=LOOKUP(5000,--MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),{1,2,3,4,5}))


Hi,

Sorry i am not dat gud with formulas like you.
Why use 5,000 not 1,000?

Biz
 
Upvote 0
Hi everyone,

Just some feedback. I eventually went with Biff's formula and modified it a bit to add 1900 or 2000 depending on whether or not the two digit date was more than 15. My final code is:

Code:
=IF(LEN(LOOKUP(5000,--MID(C2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},C2&"0123456789")),{1,2,3,4,5})))=4,
LOOKUP(5000,--MID(C2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},C2&"0123456789")),{1,2,3,4,5})),
IF(LOOKUP(5000,--MID(C2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},C2&"0123456789")),{1,2,3,4,5}))>15,
1900+LOOKUP(5000,--MID(C2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},C2&"0123456789")),{1,2,3,4,5})),
2000+LOOKUP(5000,--MID(C2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},C2&"0123456789")),{1,2,3,4,5}))))

AMAS
 
Upvote 0
Hi,

Sorry i am not dat gud with formulas like you.
Why use 5,000 not 1,000?

Biz
=LOOKUP(5000,--MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),{1,2,3,4,5}))

Let's take a look and see how this formula works.

As far as I can tell the possible data configurations will be one of the following:

Name 2 digit year
Name 2 digit year|single letter
Name 4 digit year
Name 4 digit year|single letter

Johnson 73
Johnson 73a
Johnson 1973
Johnson 1973a

So, the year portion will be contained within the last 5 characters of the string.

What we do is find the position of the first digit in the string with this:

MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789"))

Then, starting from that character, we tell it to step through that portion of the string and extract an increasing number of characters with this:

{1,2,3,4,5}

We know that the year number is within the last 5 characters of the string so we only need to look through the last 5 characters.

MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),{1,2,3,4,5})

Using that entire expression, we extract a series of TEXT substrings of increasing length until the year number is expossed. Like this:

A1 = Johnson 1973a

"1"
"19"
"197"
"1973"
"1973a"

This array of TEXT strings is then manipulated using the double unary minus "--".

--MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),{1,2,3,4,5})

This will convert the TEXT numbers to numeric numbers and will generate errors for those strings that contain characters other than number digits.

--"1" = 1
--"19" = 19
--"197" = 197
--"1973" = 1973
--"1973a" = #VALUE!

So now we have this array:

1
19
197
1973
#VALUE!

Hey, we can see the year number in there. Now we have to get it out. Here's how we do that:

=LOOKUP(5000,{1,19,197,1973,#VALUE!})

In this application the way that LOOKUP works is it will return the *LAST* numeric value in the lookup array {1,19,197,1973,#VALUE!} that is less than or equal to the lookup value 5000. Since we're dealing with year numbers we know that we will never be anywhere near year 5000 so it is GUARANTEED that the lookup value 5000 will be greater than any numeric value in the lookup array.

The *LAST* numeric value in the lookup array {1,19,197,1973,#VALUE!} that is less than or equal to the lookup value of 5000 is 1973. The "cool" thing about this is that LOOKUP will ignore the errors in the lookup array (unless they are all errors).

So, to extract the year number from the string:

A1 = Johnson 1973a

=LOOKUP(5000,--MID(A1,MIN(FIND({0,1,2,3,4,5,6,7,8,9},A1&"0123456789")),{1,2,3,4,5}))

Returns 1973
 
Upvote 0
Thanks Biff for that excellent explanation of the mechanics of the formula. Its really helpful for people don't have a superior understanding of some of the principles that are used. Keep up the good work.

AMAS
 
Upvote 0
Thanks Biff for that excellent explanation of the mechanics of the formula. Its really helpful for people don't have a superior understanding of some of the principles that are used. Keep up the good work.

AMAS
You're welcome. Thanks for the feedback! :cool:

When I get some free time later this evening I'll take a look at the formula you posted. We should be able to shorten that a bit.

Is there a certain year number that we can use to declare the century as either 1900 or 2000? For example, you know for certain that you won't have any dates earlier than say 1960 and you won't have any dates later than say 2050.
 
Upvote 0
Hi Biff,

Thanks again for your detail explanation.

Biz
 
Upvote 0
Hi Biff,

I have been working on this all day, reviewing and tweaking along the way. Here is what I have so.

Code:
=IFERROR(--IF(IFERROR(LEN(IFERROR(MID(C2,FIND(19,C2),4),MID(C2,FIND(20,C2),4))),"")=4,
IFERROR(MID(C2,FIND(19,C2),4),MID(C2,FIND(20,C2),4)),
IF(LEN(LOOKUP(5000,--MID(C2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},C2&"0123456789")),{1,2,3,4,5})))=2,
IF(LOOKUP(5000,--MID(C2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},C2&"0123456789")),{1,2,3,4,5}))>15,
1900+LOOKUP(5000,--MID(C2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},C2&"0123456789")),{1,2,3,4,5})),
2000+LOOKUP(5000,--MID(C2,MIN(FIND({0,1,2,3,4,5,6,7,8,9},C2&"0123456789")),{1,2,3,4,5}))),
"No date reported")),"No date reported")
I had to first limit the search for four-digit dates that started with 19 or 20 because I was getting wrong results when someone incorrectly writes a number that is not their birth date (e.g. 1234). Then I limited the second half to only two digit numbers to prevent it from getting these four or more digit numbers and thinking that it was year.

The problem that I still haven't resolved (not yet at least) is why these two scenarios are not working:

Code:
  * Two-digit starting with zero (e.g. Johnson 07)
  * Study name begins with number not related to date (e.g. 907 Johnson 2007)
I will have to look at the formula more closely and test out some other tweaks.

Any suggestions are of course welcome.

AMAS
 
Upvote 0

Forum statistics

Threads
1,224,522
Messages
6,179,297
Members
452,903
Latest member
Knuddeluff

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top