Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Deciphering a Coded Date.
by Allen Wyatt
(last updated September 30, 2017)
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 your 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:
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (12423) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Deciphering a Coded Date.
Program Successfully in Excel! John Walkenbach's name is synonymous with excellence in deciphering complex technical topics. With this comprehensive guide, "Mr. Spreadsheet" shows how to maximize your Excel experience using professional spreadsheet application development tips from his own personal bookshelf. Check out Excel 2013 Power Programming with VBA today!
Excel is brilliant at handling dates—as long as they aren't dates earlier than the base date used by the program. ...Discover More
Work in an industry that uses ISO standards when it comes to working with dates? You'll love the formula in this tip ...Discover More
When working with dates, it is often helpful to be able to calculate some date in the future based on a starting date. ...Discover More
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.