**Please Note: **
This article is written for users of the following Microsoft Excel versions: 2007 and 2010. 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.

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.

*ExcelTips* is your source for cost-effective Microsoft Excel training.
This tip (12423) applies to Microsoft Excel 2007 and 2010. You can find a version of this tip for the older menu interface of Excel here: **Deciphering a Coded Date**.

**Comprehensive VBA Guide** Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out *Mastering VBA for Office 2010* today!

The NETWORKDAYS worksheet function can be used to easily determine the number of work days (Monday through Friday) within a ...

Discover MoreMany businesses need to know when the last business day of the month occurs. This tip discusses several ways you can ...

Discover MoreNeed to print an elapsed date in a strange format? It's easier to do than may appear at first glance. Here's a discussion on ...

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

2017-05-15 09:52:29

Shandor

2017-05-14 21:25:22

Neil

2017-05-12 10:35:56

Shandor

2017-01-29 12:26:43

Willy Vanhaelen

Except for the first formula, you can extend the range of years with 26 upper case letters since CODE and FIND in the formulas and InStr in the UDF are case sensitive. So you can code till the year 2061.

2017-01-27 20:25:51

Harry S

Why not have all days as string

yyyy/mm/dd about 20,000 per megabyte

or

yyyy/mm/dd hh:mm:ss

or even the form of some sql searches

yyyymmddhhmmssmsms

Why these are sortable as strings

Most countries can recognise the format

a simple class can extract Y m d h m s and translate to any country or local localformat

2017-01-27 15:37:27

Harry S

Function DecodeDate3(DS$) As Date

Dim Ci%, TT() As Byte, BadDate As Boolean

' from 2 byte char representation to 1 byte ASCI

TT() = StrConv(DS, vbFromUnicode)

' ubound is 2 we hope ..3 char and zero based arrays

For Ci = 0 To UBound(TT)

' 0..9 is 48 ..57 to go to 0 1 2 3

' A... is 64 + to go to 10 11 12 as in Hexadecimal 0..F

' a... is 96 + to go to 10 11 12

Select Case TT(Ci)

Case Is > 96: TT(Ci) = TT(Ci) - 87

Case Is > 64: TT(Ci) = TT(Ci) - 55

Case 48 To 57: TT(Ci) = TT(Ci) - 48

Case Else

BadDate = True

End Select

Next Ci

If BadDate Then

DecodeDate3 = Int(Now) ' or some error message

Else

DecodeDate3 = DateSerial(2000 + TT(0), TT(1), TT(2))

End If

End Function

2016-11-24 14:39:33

Peter

Such as

=Left(vLOOKUP(C5;DATA1!A2:A225;DATA1!G2:G225,0))

or

=LOOKUP(C5;DATA1!A2:A225;left(DATA1!G2:G225,4))

I keep getting an error of bad syntax

Thanks

2013-01-12 15:46:11

Peter Atherton

Text Code Char Function

0 48 0

9 57 9

A 65 A

Z 90 Z

a 97 a

z 122 z

The inverse is the CHAR Function.

2013-01-06 11:38:12

Juan

What's the meaning of the CODE function?

2013-01-05 21:11:35

Peter Atherton

If this can be arranged then the following would work:

=DATE(HEX2DEC(LEFT(A16,FIND(",",A16)-1))+2000,HEX2DEC(MID(A16,FIND(",",A16)+1,1)),HEX2DEC(MID(A16,FIND(",",A16,FIND(",",A16)+1)+1,1234)))

This gives as far as I've tested correct dates from 1/1/2000 (0,1,1)to 31/12/2099 (63,c,1f) when it would be easier to change the base date to 2100

2013-01-05 18:51:40

Peter Atherton

The best solution IMHO is to change the Find Function publshed by Allan to Search as in:

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

Anyway Luis mentioned fields in the original post and if this translates to columns I think that the dates use Hexadecimal notation and will sometimes have more than one character per date part e.g. 10 for year 2016. If so we should be using HEX2DEC.

Any thoughts

2013-01-05 15:01:52

Michael Avidan - MVP

It should read:

=DATE(2000+MOD(CODE(LEFT(A1,1))-48,39),MOD(CODE(MID(A1,2,1))-48,39),MOD(CODE(RIGHT(A1,1))-48,39))

Michael Avidan

“Microsoft®” MVP – Excel

ISRAEL

2013-01-05 14:23:36

Michael Avidan - MVP

Thanks for bringing this to my attantion.

My "new" formula is now not much shorten than the formula that bypasses the equivalence tables.

=DATE(39*(LEFT(M1)<"9")+CODE(LEFT(M1))+1913,IF(MID(M1,2,1)>"9",CODE(MID(M1,2,1))-87,MID(M1,2,1)),IF(RIGHT(M1)>"9",CODE(RIGHT(M1))-87,RIGHT(M1)))

Still, Jan-Willems formula returns an error (or I'm doing something wrong).

Michael Avidan

“Microsoft®” MVP – Excel

ISRAEL

2013-01-05 13:46:36

Willy Vanhaelen

2013-01-05 11:07:31

Michael Avidan - MVP

Please check your proposed formula for dates like: cdc and bdk.

Michael Avidan

“Microsoft®” MVP – Excel

ISRAEL

2013-01-05 10:56:38

Michael Avidan - MVP

=DATE(CODE(LEFT(A1))+1913,CODE(MID(A1,2,1))-87,IF(RIGHT(A1)>"9",CODE(RIGHT(A1))-87,RIGHT(A1)))

By the way the formula:

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

returns an Error for: cdc and bdk.

Michael Avidan

“Microsoft®” MVP – Excel

ISRAEL

2013-01-05 06:51:12

Jan-Willem Lankhaar

=DATE(2000+MOD(CODE(LEFT(A1,1)-48,39),MOD(CODE(MID(A1,2,1))-48,39),MOD(CODE(RIGHT(A1,1))-48,39)

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.

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

Copyright © 2017 Sharon Parq Associates, Inc.

## Comments