XL98: Date Returned in a Macro Is Four Years Too Early

ID: Q186005

The information in this article applies to:

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

A macro returns a date that is four years and one day earlier when you select 1904 Date System in the Calculation tab of the Preferences dialog box, and when one of the following conditions is true:

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 the Microsoft fee-based consulting line at (800) 936-5200. For more information about the support options available from Microsoft, please see the following page on the World Wide Web:

   http://www.microsoft.com/support/supportnet/refguide/

To see an example of the problem described in this article, follow these steps:

1. Start Microsoft Excel 98, and open a new workbook.

2. On the Tools menu, click Preferences, and then click the Calculation

   tab.

3. Under Workbook Options, click to select the 1904 Date System check
   box. Click OK.

4. Type the following dates in Sheet1:

      A1: 1/1/95

5. On the Tools menu, point to Macro, and click Visual Basic Editor. On the
   Insert menu of the Visual Basic Editor, click Module.

6. 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("a1").Value 'Using Value property
    DateApp = Application.Max(Range("a1:a3")) 'Using an Application
                                              'function

    DateValue2 = Range("a1").Value2 'Using the Value2 property

       Range("c1") = DateCell
       Range("d1") = " Value property"

       Range("c2") = DateApp
       Range("d2") = " Application.Max"

       Range("c3") = DateValue2
       Range("d3") = " Value2 property"

   End Sub

7. On the File menu, click Close And Return to Microsoft Excel.

8. On the Tools menu, point to Macro, and then click Macros.

9. Select GetDates1 and click Run.

The macro will return three dates. One is returned by the value property. One is returned by the Max function in Excel, and one is returned by the value2 property. Your workbook should look similar to the following:

   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 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 changing the line of code, rerun the GetDates1 Macro. The correct dates will be returned.

STATUS

Microsoft has confirmed this to be a problem in the Microsoft products listed at the beginning of this article.

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 is a new property in Visual Basic that you can use in Microsoft Office 98. 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.

REFERENCES

For more information about the 1904 date system, click Contents And Index on the Help menu (or on the Balloon Help menu if you are using a version of the Macintosh operating system earlier than 8.0), click the Index button in Microsoft Excel Help, type the following text

   1900/1904 date system

and then click Show Topics. Select the "Tips on entering dates and times" topic, and click Go To. If you are unable to find the information you need, ask the Office Assistant.

Additional query words: XL98 vba

Keywords          : kbprg kbdta xlvbainfo OffVBA 
Version           : MACINTOSH:98
Platform          : MACINTOSH
Issue type        : kbbug
Solution Type     : kbpending

Last Reviewed: May 18, 1999