Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
DiscussionsAccessExcelInfoPathOutlookPowerPointPublisherWord
DirectoryUser Groups
Related Topics
Outlook ExpressInternet ExplorerWindowsMS Server ProductsMore Topics ...

MS Office Forum / Excel / Programming / December 2007

Tip: Looking for answers? Try searching our database.

dates in macros

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
Max Scott - 15 Dec 2007 17:31 GMT
Hi

I have a problem with a filter in a macro. I recorded a macro and it gave
following code:

selection.autofilter field:=1, Criteria1:="13/12/07"

(This is date type dd/mm/yy by the way)

But when I change the criteria1 to "14/12/07" and run the macro it does not
filter correctly.

Is there something special I need to do with dates?

Thanks

Max
Ron de Bruin - 15 Dec 2007 17:38 GMT
Hi Max

Change the date format in the code to US format

MDY

Signature

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm

> Hi
>
[quoted text clipped - 13 lines]
>
> Max
Ron de Bruin - 15 Dec 2007 17:53 GMT
Use 2007 not 07

Signature

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm

> Hi Max
>
[quoted text clipped - 19 lines]
>>
>> Max
Dave Peterson - 16 Dec 2007 01:57 GMT
Just to add to Ron's response...

Ron sent me this message in a private email a few months ago:

See also Stephen his Autofilter notes in this PDF
http://www.oaltd.co.uk/ExcelProgRef/Ch22/ProgRefCh22.htm

The AutoFilter method of a Range object is a very curious beast. We are forced
to pass it strings for its filter criteria and hence must be aware of its string
handling behaviour. The criteria string consists of an operator (=, >, <, >=
etc.) followed by a value. If no operator is specified, the "=" operator is
assumed. The key issue is that when using the "=" operator, AutoFilter performs
a textual match, while using any other operator results in a match by value.
This gives us problems when trying to locate exact matches for dates and
numbers. If we use "=", Excel matches on the text that is displayed in the cell,
i.e. the formatted number. As the text displayed in a cell will change with
different regional settings and Windows language version, it is impossible for
us to create a criteria string that will locate an exact match in all locales.

There is a workaround for this problem. When using any of the other filter
criteria, Excel plays by the rules and interprets the criteria string according
to US formats. Hence, a search criterion of ">=02/01/2001" will find all dates
on or after 1st Feb, 2001, in all locales. We can use this to match an exact
date by using two AutoFilter criteria. The following code will give an exact
match on 1st Feb, 2001 and will work in any locale:


  Range("A1:D200").AutoFilter 2, ">=02/01/2001", xlAnd, "<=02/01/2001"

> Hi Max
>
[quoted text clipped - 24 lines]
> >
> > Max

Signature

Dave Peterson

Ron de Bruin - 16 Dec 2007 10:15 GMT
Hi Dave

Good morning

Thanks for posting the notes I send you (will remember to do it also the next time)

I send a bug report yesterday about it.

If you record a macro when you do it manual the macro recorder record the system
Date format and that is not working when you have another Date format as US on your system.
You must change it in the US date format MM/DD/YYYY then

Confusing

Signature

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm

> Just to add to Ron's response...
>
[quoted text clipped - 52 lines]
>> >
>> > Max
Dave Peterson - 16 Dec 2007 15:38 GMT
You should move to the USA and just live with one date format <vvbg>.

> Hi Dave
>
[quoted text clipped - 75 lines]
> >
> > Dave Peterson

Signature

Dave Peterson

Ron de Bruin - 16 Dec 2007 16:29 GMT
> You should move to the USA and just live with one date format <vvbg>.

Do you have a room for me Dave <g>

Signature

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm

> You should move to the USA and just live with one date format <vvbg>.
>
[quoted text clipped - 77 lines]
>> >
>> > Dave Peterson
Gord Dibben - 16 Dec 2007 17:52 GMT
Move to Canada.  We'll take anybody<g>

Gord

>> You should move to the USA and just live with one date format <vvbg>.
>
>Do you have a room for me Dave <g>
Ron de Bruin - 16 Dec 2007 18:06 GMT
> Move to Canada.  We'll take anybody<g>

LOL

When I was 8 years old I visit Canada with my parents.
It is a beautiful country Gord.

Signature

Regards Ron de Bruin
http://www.rondebruin.nl/tips.htm

> Move to Canada.  We'll take anybody<g>
>
[quoted text clipped - 3 lines]
>>
>>Do you have a room for me Dave <g>
Dave Peterson - 16 Dec 2007 18:11 GMT
And they're very polite!

Do you know how to get 20 Canadian's out of a pool?

You ask them.

> > Move to Canada.  We'll take anybody<g>
>
[quoted text clipped - 7 lines]
> Regards Ron de Bruin
> http://www.rondebruin.nl/tips.htm
R - 15 Dec 2007 23:21 GMT
Hi,

Try this kind of construction

Sub AutofilterDate()
   Dim MyDate, FindDate As Date
   MyDate = "13/12/2007"
   FindDate = Year(MyDate) & "/" & Month(MyDate) & "/" & Day(MyDate)
   Selection.AutoFilter field:=1, Criteria1:=FindDate
End Sub

Reijo
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.