Microsoft KB Archive/73673

= Microsoft Knowledge Base =

XL: Dates Assume Twentieth Century in Microsoft Excel
Last reviewed: July 16, 1997

Article ID: Q73673

The information in this article applies to:


 * Microsoft Excel for Windows, versions 2.x, 3.x, 4.x, 5.0, 5.0c
 * Microsoft Excel for Windows 95, version 7.0
 * Microsoft Excel 97 for Windows
 * Microsoft Excel for the Macintosh, versions 2.x, 3.x, 4.x, 5.0, 5.0a

SUMMARY
If you enter a date in Microsoft Excel, but you do not specify the century in which the date occurs, Microsoft Excel may assume that the date occurs in the twentieth century (1900s).

You can create a macro to change 20th century dates to 21st century dates.

NOTE: The latest date that Microsoft Excel versions 2.1, 3.0, 4.0, 5.0, and 7.0 will accept is December 31, 2078. The latest date that Microsoft Excel 97 will accept is December 31, 9999.

MORE INFORMATION
In Microsoft Excel version 2.1, the twentieth century is always assumed, but in Microsoft Excel versions 3.0, 4.0, 5.0, and 7.0, the twentieth century is assumed if the year portion of the date is greater than 19 (for example, 1/1/20). In Microsoft Excel 97, the twentieth century is assumed if the year portion of the date is greater than 29.

Microsoft provides examples of Visual Basic for Applications procedures for illustration only, without warranty either expressed or implied, including, but not limited to the implied warranties of merchantability and/or fitness for a particular purpose. The Visual Basic procedures in this article are provided 'as is' and Microsoft does not guarantee that they can be used in all situations. While Microsoft support engineers can help explain the functionality of a particular macro, they will not modify these examples to provide added functionality, nor will they help you construct macros to meet your specific needs. If you have limited programming experience, you may want to consult one of the Microsoft Solution Providers. Solution Providers offer a wide range of fee-based services, including creating custom macros. For more information about Microsoft Solution Providers, call Microsoft Customer Information Service at (800) 426-9400.

This procedure takes a 20th century date and adds to the year, so the year is converted to 21st century.

CAUTION: This procedure will remove the old date and overwrite it with the new date.

Visual Basic Procedure
  In a Visual Basic module, enter the following code: Sub CenturyConvert

'Run the following subroutine until you reach a blank cell. While Not (ActiveCell = &quot;&quot;)

'Place the contents of the active cell into the variable ac        ac = ActiveCell

'Ignore if the year is '79 or later because Microsoft Excel 'cannot handle dates later than 1/31/2078. If Right(CStr(Year(ac)), 2) < 79 And _ Left(Application.Version, 1) < 8 Then

'Add 2000 to the year, rebuild the date and let temp hold 'the date. temp = DateValue(DateSerial(2000 + (Year(ac) Mod 100), _ Month(ac), Day(ac)))

'Return the 21st century date to the new cell on the worksheet.

ActiveCell.Value = CDbl(temp)

ElseIf Left(Application.Version, 1) >= 8 Then

'Add 2000 to the year, rebuild the date and let temp hold 'the date. temp = DateValue(DateSerial(2000 + (Year(ac) Mod 100), _ Month(ac), Day(ac)))

'Return the 21st century date to the new cell on the worksheet.

ActiveCell.Value = CDbl(temp) End If

'Move down to the next cell. ActiveCell.Offset(1, 0).Select Wend

End Sub  To run the above code, select the first cell in the column containing the dates, choose Macro from the Tools menu, select the CenturyConvert macro, and choose Run.

Microsoft Excel Version 4.0 Macro
In Microsoft Excel, you can use the DATE function for dates with years from 1900 to 2078. The following macro converts each date in a column to the 21st century, as long as the year portion of the date is less than 79, regardless of which century is currently defined for each:

A1: =WHILE(NOT(ISBLANK(ACTIVE.CELL))) A2: AC=ACTIVE.CELL A3: =FORMULA(DATE(2000+MOD(YEAR(AC),100),MONTH(AC),DAY(AC))) A4: =SELECT(&quot;R[1]C&quot;) A5: =NEXT A6: =RETURN Before you run this macro, select the cell at the top of a column of dates. The macro stops at the first blank cell in the column.

Use the following formula to convert a date in a specific cell (replace A1 with the appropriate cell reference):

=DATE(2000+MOD(YEAR(A1),100),MONTH(A1),DAY(A1)) Format the cell containing the above formula to an appropriate date format. Again, the year portion of the date must be less than 79, or you will receive the #NUM! error value.