XL97: Format Function May Return an Incorrect Date

ID: Q172588

The information in this article applies to:

SYMPTOMS

In Microsoft Excel, if you run a Visual Basic for Applications macro that uses the Format function with a date, you may notice the following problems:

CAUSE

These problems may occur if are using regional settings that use a date order of either day-month-year or year-month-day.

STATUS

Microsoft has confirmed this to be a problem in Microsoft Excel 97.

MORE INFORMATION

In Microsoft Excel, dates contain three elements: a year, a month, and a day. The order in which these elements are displayed in a date depends on the regional settings in use on the computer; these regional settings vary from country to country. Microsoft Excel mainly uses the three date orders that are listed in the following table.

   Order            July 5, 1997 is represented as
   -----------------------------------------------

   month-day-year   7/5/97
   day-month-year   5/7/97
   year-month-day   97/7/5

Under Microsoft Windows 95 and Microsoft Windows NT, the first order, month-day-year, is used by the following regional settings:

   English (United States)
   Spanish (Dominican Republic)
   Spanish (Panama)

If you are using any of these three regional settings, the problems described in the "Symptoms" section in this article do not occur.

If you are using any other regional settings and you run a Visual Basic macro that uses the Format function to insert dates into cells or to display a date in a message box, you may encounter the problems described in this article.

Example

You can demonstrate these problems by following these steps:

1. On the Start menu, point to Settings, and click Control Panel.

2. Double-click Regional Settings. Select the Regional Settings tab.

3. In the list of regional settings, click "English (British)," and then

   click OK. When you are prompted, restart the computer.

4. Start Microsoft Excel 97 and create a new workbook.

5. On the Tools menu, point to Macro, and click Visual Basic Editor. Then,

   click Module on the Insert menu.

6. Type the following code into the new module:

      Sub Test()

          Range("A1").Value = Format(Date, "General Date")
          Range("A2").Value = Format(Date, "Long Date")
          Range("A3").Value = Format(Date, "Medium Date")
          Range("A4").Value = Format(Date, "Short Date")

      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. Click Test

   and click Run.

Dates are inserted into cells A1:A4 in the worksheet. Note the following behavior: NOTE: Be sure to switch the regional settings back to the default settings when you are done.

Additional query words: XL97 sr1 sr-1 australian canadian new zealand french german italian british norwegian portuguese swedish danish ireland south africa argentina chile colombia paraguay uruguay peru mexican costa rica ecuador guatemala peru venezuela brazilian swiss icelandic indonesian afrikaans basque catalan dutch belgian finnish luxembourg austrian liechtenstein caribbean jamaica nynorsk bokmal sr1

Keywords          : kbdta kbdtacode xlformat KbVBA 
Version           : WINDOWS:97
Platform          : WINDOWS
Issue type        : kbprb

Last Reviewed: January 9, 1999