Microsoft KB Archive/248669
Article ID: 248669
Article Last Modified on 10/11/2006
- Microsoft Excel 2000 Standard Edition
This article was previously published under Q248669
When you run a recorded Microsoft Visual Basic for Applications macro (Sub procedure) that performs an AutoFilter, dates that meet the criteria are not filtered.
This behavior occurs when you perform the following steps:
- You configure the regional settings in the operating system to display the short date format in any format other than the default setting.
- You type dates in one or more cells in a workbook, including the day, month, and year, with hyphen (-) or slash mark (/) separators, and you do not specify a specific date format.
- You record a macro by using the AutoFilter command.
- You select a date for part of the AutoFilter criteria.
- You run the recorded macro.
To work around this behavior, use either of the following methods:
- Change the date format of the recorded macro to the default short date format of the operating system.
- Format the dates in the worksheet and recorded code with a custom date format.
Method 1: Change the Date Format of the Recorded Code
Change the value that is assigned to the Criteria1 argument or the Criteria2 argument of the AutoFilter method to match the default short date format of the operating system.
For example, in Microsoft Windows 95, Microsoft Windows 98, or Microsoft Windows NT 4.0, if the line of code is the following
Selection.AutoFilter Field:=1, Criteria1:="2/2/98"
and the cell that contains the date in the AutoFilter list is formatted as "m/d/yyyy", change the line of code to the following:
Selection.AutoFilter Field:=1, Criteria1:="2/2/1998"
Method 2: Change the Date Format of the Code and Worksheet Cells
Change the format of the dates to a different custom date format, and change the value assigned to the Criteria1 argument or the Criteria2 argument of the AutoFilter method to match the same custom date format. Follow these steps to perform the task:
- In the worksheet, select the dates in the list.
- On the Format menu, click Cells.
- On the Number tab, click Custom in the Category list.
- Type a new date format in the Type box (for example, MM/dd/yyyy), and then click OK.
Change the value assigned to the Criteria1 argument or the Criteria2 argument of the AutoFilter method to match the date format of the dates in the worksheet list.
For example, if the cells are formatted as MM/dd/yyyy, the line of code is the following:
Selection.AutoFilter Field:=1, Criteria1:="02/02/1998"
Microsoft has confirmed that this is a problem in the Microsoft products that are listed at the beginning of this article.
In Microsoft Windows 95, Microsoft Windows 98, and Microsoft Windows NT 4.0, the default short date format is "M/d/yy".
In Microsoft Windows 2000, the default short date format is "M/d/yyyy".
For additional information about other issues with dates in AutoFilters and recorded macros, click the article number below to view the article in the Microsoft Knowledge Base:
248235 XL2000: Recorded AutoFilter Macro Does Not Filter Dates Incorrectly
Additional query words: XL2000 Y2K vba
Keywords: kbbug kbpending KB248669