Please Note: This article is written for users of the following Microsoft Excel versions: 97, 2000, 2002, and 2003. If you are using a later version (Excel 2007 or later), this tip may not work for you. For a version of this tip written specifically for later versions of Excel, click here: Deciphering a Coded Date.

Deciphering a Coded Date

Written by Allen Wyatt (last updated April 14, 2018)
This tip applies to Excel 97, 2000, 2002, and 2003


2

Luis receives information in which dates are coded such that years, months, and days are replaced with a single character each. For each field, the numbers 1 to 9 are used and after that letters, from a=10, b=11, ... v=31. For example, the date code 'bc2' means b=11 (the year 2011), c=12 (the month), and day=2. Luis wonders if a function can be devised to replace the coded date with a common date format, such as dd/mm/yyyy.

There are actually several ways you could go about solving this problem. One way is to set up "equivalence tables" within a worksheet, where the left column includes a code character and the right indicates the numeric value that is associated with that character. You could then fashion a formula that uses VLOOKUP to find the values and convert the results into a date.

As an example, create you equivalence table in some unused cells to the right of your data. In my case, I put the table in columns P and Q. In column P I put the code characters, 1 through 9 and a through z. (Make sure you precede the digits 1 through 9 with an apostrophe so they are stored as text rather than as numbers.) In column Q I put the numbers 1 through 35. This entire range (P1:Q35) I then gave a name of DateTable. Here is the formula, then, that will return a decoded date for a coded date stored in cell A1:

=DATE(2000+VLOOKUP(LEFT(A1,1),DateTable,2,0),
VLOOKUP(MID(A1,2,1),DateTable,2,0),VLOOKUP(RIGHT(A1,1),
DateTable,2,0))

Remember that this is a single, continuous formula. Another technique is to bypass the equivalence tables altogether and instead use a formula to do the conversion. The following is an example that will decode a date in cell A1:

=DATE(2000+FIND(LEFT(A1,1),"123456789abcdefghijklmnopqrstuvwxyz"),
FIND(MID(A1,2,1),"123456789abc"),FIND(MID(A1,3,1),
"123456789abcdefghijklmnopqrstuv"))

This formula uses the FIND function to translate from the code character to a value, and then these values are used in the DATE function to return the desired date. Another possible formula relies, instead, on character code values to create the date:

=DATE(2000+CODE(MID(A1,1,1))-87+(CODE(MID(A1,1,1))<58)*39,CODE(MID(A1,2,1))-87+(CODE(MID(A1,2,1))<58)*39,CODE(MID(A1,3,1))-87+(CODE(MID(A1,3,1))<58)*39)

Finally, you could create a user-defined function to return the decoded date. The following is just a simple example; it looks at each character and converts it to a numeric value that is then used with the DateSerial function to create an Excel date serial number:

Function DecodeDate(datecode As String)
    Const X = "123456789abcdefghijklmnopqrstuvwxyz"
    Dim D As Integer
    Dim M As Integer
    Dim Y As Integer

    Application.Volatile
    D = InStr(X, Right(datecode, 1))
    M = InStr(X, Mid(datecode, 2, 1))
    Y = 2000 + InStr(X, Left(datecode, 1))
    DecodeDate = DateSerial(Y, M, D)
End Function

It should be pointed out, as well, that regardless of the approach you use, there is an inherent flaw in your date codes. The year uses the code values 1 through 9 and a through z. This means that the date code can be one of 35 possible values. When added to the year 2000 (the base year for how you described the code), that means that the maximum year value that can be coded is 2035. Any date after that year will not work with this coding.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12422) applies to Microsoft Excel 97, 2000, 2002, and 2003. You can find a version of this tip for the ribbon interface of Excel (Excel 2007 and later) here: Deciphering a Coded Date.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Collating Copies

If you are printing more than one copy of a multi-page document, it would be great if Word could print them in complete ...

Discover More

Getting Rid of Blank Labels in a Filtered Merge

If you are filtering a mail merge in Excel, and you get blank labels in the printout in Word, chances are good that ...

Discover More

Turning Off Track Changes without Unsharing

The Track Changes tool in Excel can be helpful, but it can also be aggravating because it doesn't allow you to use it on ...

Discover More

Excel Smarts for Beginners! Featuring the friendly and trusted For Dummies style, this popular guide shows beginners how to get up and running with Excel while also helping more experienced users get comfortable with the newest features. Check out Excel 2013 For Dummies today!

More ExcelTips (menu)

Calculating a Date Five Days before the First Business Day

Excel allows you to perform all sorts of calculations using dates. A good example of this is using a formula to figure ...

Discover More

Ages in Years and Months

Calculating an age is a common task when working with dates. If you want to figure out the number of years and months ...

Discover More

Displaying a Number as Years and Months

How do you display a number of years, such as 3.67 years, as a number of years and months? It's simple to do with a ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] (all 7 characters, in the sequence shown) in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is two more than 7?

2018-04-14 05:56:37

Rick Rothstein

Of course what I posted earlier does not work. This is what the UDF (user defined function) should have been...

[code]
Function DecodeDate(Code As String) As Date
Const X = "123456789abcdefghijklmnopqrstuvwxyz"
DecodeDate = 2000 + InStr(X, Left(Code, 1)) & "-" & Format(InStr(X, Mid(Code, 2, 1)), "00") & "-" & Format(InStr(X, Right(Code, 1)), "00")
End Function
[/code]


2018-04-14 05:26:06

Rick Rothstein

Here is another way to write the UDF (user defined function)...

[code]
Function DecodeDate(Code As String) As Date
Const X = "123456789abcdefghijklmnopqrstuvwxyz"
DecodeDate = "20" & InStr(X, Left(Code, 1)) & "-" & InStr(X, Mid(Code, 2, 1)) & "-" & InStr(X, Right(Code, 1))
End Function
[/code]


This Site

Got a version of Excel that uses the menu interface (Excel 97, Excel 2000, Excel 2002, or Excel 2003)? This site is for you! If you use a later version of Excel, visit our ExcelTips site focusing on the ribbon interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.