Microsoft KB Archive/274238

= Date returned in a macro is four years too early =

Article ID: 274238

Article Last Modified on 1/29/2007

-

APPLIES TO


 * Microsoft Excel X for Mac
 * Microsoft Excel 2001 for Mac
 * Microsoft Excel 98 for Macintosh
 * Microsoft Excel 2004 for Mac

-



This article was previously published under Q274238



SYMPTOMS
When you run a macro that uses a date from a worksheet cell, the date returned by the macro may be four years and one day earlier than the actual date.



CAUSE
This behavior occurs when you select the 1904 Date System check box on the Calculation tab of the Preferences dialog box, and when one of the following conditions is true:
 * You declare the Visual Basic variable as Date by using the DIM statement.

-and-
 * The macro uses a worksheet function in the Microsoft Excel application library when it stores the date.

-or-
 * The macro uses the Value2 property when it stores the date.



WORKAROUND
Microsoft provides programming examples for illustration only, without warranty either expressed or implied. This includes, but is not limited to, the implied warranties of merchantability or fitness for a particular purpose. This article assumes that you are familiar with the programming language that is being demonstrated and with the tools that are used to create and to debug procedures. Microsoft support engineers 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 requirements. To see an example of the behavior described in this article, follow these steps:  Start Microsoft Excel, and open a new workbook. On the Edit menu (or Excel menu in Excel X), click Preferences, and then click the Calculation tab. Under Workbook options, click to select the 1904 Date System check box. Click OK.  Type the following dates in Sheet1:   A1: 1/1/95  On the Tools menu, point to Macro, and then click Visual Basic Editor. On the Insert menu of the Visual Basic Editor, click Module.  Type the following code into the module sheet: Sub GetDates1 ' The variables must be declared as Date and the Date system ' must be set to 1904 to display the problem.

Dim DateCell As Date, DateApp As Date, DateValue2 As Date

'Value property always works correctly. DateCell = Range(&quot;a1&quot;).Value 'Using Value property DateApp = Application.Max(Range(&quot;a1:a3&quot;)) 'Using an Application 'function.

DateValue2 = Range(&quot;a1&quot;).Value2 'Using the Value2 property

Range(&quot;c1&quot;) = DateCell Range(&quot;d1&quot;) = &quot; Value property&quot;

Range(&quot;c2&quot;) = DateApp Range(&quot;d2&quot;) = &quot; Application.Max&quot;

Range(&quot;c3&quot;) = DateValue2 Range(&quot;d3&quot;) = &quot; Value2 property&quot;

End Sub </li> On the File menu, click Close and Return to Microsoft Excel.</li> On the Tools menu, point to Macro, and then click Macros.</li> Select GetDates1 and click Run.</li></ol>

The macro returns three dates. One is returned by the Value property, one is returned by the Max function, and one is returned by the Value2 property. Your workbook should look similar to the following: <pre class="fixed_text">  C1: 1/1/95   D1: Value Property C2: 12/31/90 D2: Application.Max C3: 12/31/90 D3: Value2 Property To get this example to return the correct dates, declare the all variables as Variant. To do this, change the following line of code from the previous example Dim DateCell As Date, DateApp As Date, DateValue2 As Date

to

Dim DateCell As Variant, DateApp As Variant, DateValue2 As Variant

After you change the line of code, rerun the GetDates1 macro. The correct dates are returned.

<div class="status_section">

STATUS
Microsoft has confirmed that this is a problem in the Microsoft products that are listed in the &quot;Applies to&quot; section.

<div class="moreinformation_section">

MORE INFORMATION
Visual Basic for Applications does not automatically detect the 1904 date system and convert the date as necessary. If a user selects the 1904 date system in Microsoft Excel, and runs a macro that reads a date from a worksheet cell, the difference may be four years and one day (the extra one day accounts for the leap year). For example, a date of 9/1/96 in the 1904 date system may return a date of 8/31/92.

The Value2 property stores variables in the same way as the Value property except that it does not use the Currency or Date data types.

<div class="references_section">