XL: Date Inserted by Recorded Macro May Be in Wrong Century (182766)



The information in this article applies to:

  • Microsoft Excel for Windows 95
  • Microsoft Excel for Windows 95 7.0a
  • Microsoft Excel for Windows 5.0
  • Microsoft Excel for Windows 5.0c
  • Microsoft Excel for Windows NT 5.0
  • Microsoft Excel for the Macintosh 5.0
  • Microsoft Excel for the Macintosh 5.0a

This article was previously published under Q182766

SYMPTOMS

In the versions of Microsoft Excel listed at the beginning of this article, if you enter a date in a cell while you are recording a Visual Basic for Applications macro, the macro may incorrectly enter the date when you run the macro.

Specifically, the date may be in the wrong century; for example, instead of 1/1/2020, the date appears as 1/1/1920.

CAUSE

This problem occurs because, when you enter a date while recording a macro, the recorded code contains a two-digit year number instead of a four-digit year number. For example, the recorded code is similar to the following:
   ActiveCell.FormulaR1C1 = "1/1/20"
				
When you execute this line of code, Excel inserts the date into the cell as the two-digit year number, 20. As a result, the date falls between 1920 and 2019. This behavior occurs regardless of the date you actually entered in the cell while recording the macro.

For additional information about how Excel works with two-digit year numbers, click the following article number to view the article in the Microsoft Knowledge Base:

302768 How Microsoft Excel works with two-digit year numbers

WORKAROUND

Microsoft provides programming examples 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. This article assumes that you are familiar with the programming language being demonstrated and the tools used to create and debug procedures. Microsoft support professionals can help explain the functionality of a particular procedure, but they will not modify these examples to provide added functionality or construct procedures to meet your specific needs. If you have limited programming experience, you may want to contact a Microsoft Certified Partner or the Microsoft fee-based consulting line at (800) 936-5200. For more information about Microsoft Certified Partners, please visit the following Microsoft Web site: For more information about the support options that are available and about how to contact Microsoft, visit the following Microsoft Web site:
To work around this problem, modify the recorded code. For example, if the line of code is the following
ActiveCell.FormulaR1C1 = "1/1/20"
				
change it to the following:
ActiveCell.FormulaR1C1 = "1/1/2020"   'January 1, 2020
				
After you do this, the code inserts the correct date into the active cell when you run the macro.

STATUS

Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.

Modification Type:MinorLast Reviewed:10/11/2006
Keywords:kbbug KB182766