A Microsoft Excel forum. ExcelBanter

If this is your first visit, be sure to check out the FAQ by clicking the link above. You may have to register before you can post: click the register link above to proceed. To start viewing messages, select the forum that you want to visit from the selection below.

Go Back   Home » ExcelBanter forum » Excel Newsgroups » Excel Programming
Site Map Home Register Authors List Search Today's Posts Mark Forums Read Web Partners

Autofilter problem



 
 
Thread Tools Display Modes
  #1  
Old July 28th 12, 10:17 PM posted to microsoft.public.excel.programming
Ludo
external usenet poster
 
Posts: 74
Default Autofilter problem

Hi,
Excel 2007/2010
I'm trying to use the autofilter on a range using the actual date as a criteria.
I have following code but it fails on the indicated code line.

once i can autofilter for the actual date, then i need to select the apropriate data from columns B (visible rows) to send them to a recipient with mail.
For sending mail, i'll visit the site of Ron De Bruin.

I expect that RngToFilter is a valid range, so hoping i can address this imediately in the code as follow:
RngToFilter.AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(2, strToday)'<<<<<< error 1004 Method Autofilter from Class Range fails
but it fails.
Any idea what i'm doing wrong?

Sub FilterByToday()
'
Dim w As Worksheet
Dim RngToFilter As Range
Dim strToday As String
'get actual date
strToday = Date

Set w = Worksheets("Sheet1")
With w
'reset the autofilter mode
.AutoFilterMode = False
'set range to filter to the current region without the headers
Set RngToFilter = .Cells.CurrentRegion
RngToFilter.Offset(1, 0).Resize(RngToFilter.Rows.Count - 1, RngToFilter.Columns.Count).Select
Set RngToFilter = Selection
'use this range to filter on the actual date
RngToFilter.AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(2, strToday)'<<<<<< error 1004 Method Autofilter from Class Range fails

End With
'reset the autofilter mode
w.AutoFilterMode = False
'clean up
Set RngToFilter = Nothing
Set w = Nothing
End Sub

here's my test data:

Date article # serialnumber Description
27/07/2012 1 a aaaaaa
27/07/2012 2 b aaaaaa
27/07/2012 3 c ssssss
27/07/2012 4 d ddddd
27/07/2012 5 e cdccccc
27/07/2012 6 f xvbvfb
28/07/2012 1 a qdffg
28/07/2012 2 b svght
28/07/2012 3 c scdbf
28/07/2012 4 d ggjku
28/07/2012 5 e fgrrjk
28/07/2012 6 f xcvbfbn
28/07/2012 7 g adgvfhgj

Any help welcome,

Regards,
Ludo
Ads
  #2  
Old July 28th 12, 10:37 PM posted to microsoft.public.excel.programming
Claus Busch
external usenet poster
 
Posts: 931
Default Autofilter problem

Hi Ludo,

Am Sat, 28 Jul 2012 14:17:07 -0700 (PDT) schrieb Ludo:

> I'm trying to use the autofilter on a range using the actual date as a criteria.
> I have following code but it fails on the indicated code line.
>
> once i can autofilter for the actual date, then i need to select the apropriate data from columns B (visible rows) to send them to a recipient with mail.
> For sending mail, i'll visit the site of Ron De Bruin.
>
> I expect that RngToFilter is a valid range, so hoping i can address this imediately in the code as follow:
> RngToFilter.AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(2, strToday)'<<<<<< error 1004 Method Autofilter from Class Range fails
> but it fails.


Try:

Sub FilterByToday()

Dim wsh As Worksheet
Dim RngToFilter As Range

Set wsh = Worksheets("Sheet1")
With wsh
.AutoFilterMode = False
Set RngToFilter = .Cells.CurrentRegion
RngToFilter.AutoFilter Field:=1, Criteria1:=xlFilterToday, _
Operator:=xlFilterDynamic
End With

Set RngToFilter = Nothing
Set wsh = Nothing
End Sub

Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #3  
Old July 29th 12, 04:08 AM posted to microsoft.public.excel.programming
Ron Rosenfeld[_2_]
external usenet poster
 
Posts: 888
Default Autofilter problem

On Sat, 28 Jul 2012 14:17:07 -0700 (PDT), Ludo > wrote:

>Hi,
>Excel 2007/2010
>I'm trying to use the autofilter on a range using the actual date as a criteria.
>I have following code but it fails on the indicated code line.
>
>once i can autofilter for the actual date, then i need to select the apropriate data from columns B (visible rows) to send them to a recipient with mail.
>For sending mail, i'll visit the site of Ron De Bruin.
>
>I expect that RngToFilter is a valid range, so hoping i can address this imediately in the code as follow:
> RngToFilter.AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(2, strToday)'<<<<<< error 1004 Method Autofilter from Class Range fails
>but it fails.
>Any idea what i'm doing wrong?
>
>Sub FilterByToday()
>'
> Dim w As Worksheet
> Dim RngToFilter As Range
> Dim strToday As String
>'get actual date
> strToday = Date
>


I'm pretty sure that the date needs to be in the format of m/d/yyyy. VBA is US-Centric
So try strToday = format(date, "m/d/yyyy")
  #4  
Old July 30th 12, 12:56 PM posted to microsoft.public.excel.programming
Ludo
external usenet poster
 
Posts: 74
Default Autofilter problem

Op zaterdag 28 juli 2012 23:37:25 UTC+2 schreef Claus Busch het volgende:
> Hi Ludo,
>
>
>
> Am Sat, 28 Jul 2012 14:17:07 -0700 (PDT) schrieb Ludo:
>
>
>
> > I'm trying to use the autofilter on a range using the actual date as a criteria.

>
> > I have following code but it fails on the indicated code line.

>
> >

>
> > once i can autofilter for the actual date, then i need to select the apropriate data from columns B (visible rows) to send them to a recipient with mail.

>
> > For sending mail, i'll visit the site of Ron De Bruin.

>
> >

>
> > I expect that RngToFilter is a valid range, so hoping i can address this imediately in the code as follow:

>
> > RngToFilter.AutoFilter Field:=1, Operator:=xlFilterValues, Criteria2:=Array(2, strToday)'<<<<<< error 1004 Method Autofilter from Class Range fails

>
> > but it fails.

>
>
>
> Try:
>
>
>
> Sub FilterByToday()
>
>
>
> Dim wsh As Worksheet
>
> Dim RngToFilter As Range
>
>
>
> Set wsh = Worksheets("Sheet1")
>
> With wsh
>
> .AutoFilterMode = False
>
> Set RngToFilter = .Cells.CurrentRegion
>
> RngToFilter.AutoFilter Field:=1, Criteria1:=xlFilterToday, _
>
> Operator:=xlFilterDynamic
>
> End With
>
>
>
> Set RngToFilter = Nothing
>
> Set wsh = Nothing
>
> End Sub
>
>
>
> Regards
>
> Claus Busch
>
> --
>
> Win XP PRof SP2 / Vista Ultimate SP2
>
> Office 2003 SP2 /2007 Ultimate SP2


Hi Claus,

Thanks for the info.
It works fine.

Additional question:
How did you know that you can use Criteria1:=xlFilterToday?
I can't find this back in the Help for Excel.
I guess that this knowledge belongs to the real experts .

Where can i find more 'expert' info abouth this topic (Autofilter)?

Regards,
Ludo
  #5  
Old July 30th 12, 01:00 PM posted to microsoft.public.excel.programming
Ludo
external usenet poster
 
Posts: 74
Default Autofilter problem

<SNIP>
>
>
>
> I'm pretty sure that the date needs to be in the format of m/d/yyyy. VBA is US-Centric
>
> So try strToday = format(date, "m/d/yyyy")


Hi Ron,

Tried it in my original code dut get still the same error.
The sollution from Claus works pritty well.

Anyhow thanks a lot for your remark.
Working with date and time is sometimes a tricky issue.

Regards,
Ludo
  #6  
Old July 30th 12, 01:08 PM posted to microsoft.public.excel.programming
Ludo
external usenet poster
 
Posts: 74
Default Autofilter problem

<SNIP>
>
> Try:
>
>
>
> Sub FilterByToday()
>
>
>
> Dim wsh As Worksheet
>
> Dim RngToFilter As Range
>
>
>
> Set wsh = Worksheets("Sheet1")
>
> With wsh
>
> .AutoFilterMode = False
>
> Set RngToFilter = .Cells.CurrentRegion
>
> RngToFilter.AutoFilter Field:=1, Criteria1:=xlFilterToday, _
>
> Operator:=xlFilterDynamic
>
> End With
>
>
>
> Set RngToFilter = Nothing
>
> Set wsh = Nothing
>
> End Sub
>
>
>
> Regards
>
> Claus Busch
>
> --
>
> Win XP PRof SP2 / Vista Ultimate SP2
>
> Office 2003 SP2 /2007 Ultimate SP2


Hi Claus,

It works fine.
Thanks for your help.

Additional question:
How did you know that you can use Criteria1:=xlFilterToday?
Can't find this back in the Help for Excel
I guess that this is 'expert' knowledge

Where can i find more of this 'expert' info?
Online, in books, ...

Regards,
Ludo
  #7  
Old July 30th 12, 01:19 PM posted to microsoft.public.excel.programming
Claus Busch
external usenet poster
 
Posts: 931
Default Autofilter problem

Hi Ludo,

Am Mon, 30 Jul 2012 05:08:50 -0700 (PDT) schrieb Ludo:

> Additional question:
> How did you know that you can use Criteria1:=xlFilterToday?
> Can't find this back in the Help for Excel
> I guess that this is 'expert' knowledge


look in the VBA help for:
XlDynamicFilterCriteria-Enumeration


Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #8  
Old July 30th 12, 01:23 PM posted to microsoft.public.excel.programming
Ludo
external usenet poster
 
Posts: 74
Default Autofilter problem

Op maandag 30 juli 2012 14:19:16 UTC+2 schreef Claus Busch het volgende:
> Hi Ludo,
>
>
>
> Am Mon, 30 Jul 2012 05:08:50 -0700 (PDT) schrieb Ludo:
>
>
>
> > Additional question:

>
> > How did you know that you can use Criteria1:=xlFilterToday?

>
> > Can't find this back in the Help for Excel

>
> > I guess that this is 'expert' knowledge

>
>
>
> look in the VBA help for:
>
> XlDynamicFilterCriteria-Enumeration
>
>
>
>
>
> Regards
>
> Claus Busch
>
> --
>
> Win XP PRof SP2 / Vista Ultimate SP2
>
> Office 2003 SP2 /2007 Ultimate SP2


Hi Claus,

Thanks for the verry fast reply.
I will check immediately.

Regards,
Ludo
  #9  
Old July 30th 12, 01:51 PM posted to microsoft.public.excel.programming
Claus Busch
external usenet poster
 
Posts: 931
Default Autofilter problem

Hi Ludo,

Am Mon, 30 Jul 2012 05:23:54 -0700 (PDT) schrieb Ludo:

> Thanks for the verry fast reply.
> I will check immediately.


like Ron posted, your date must have the format MM/DD/YYYY.
But I think you don't have a english version of excel.
Formatting the date is not usefull therefore you have to create the
string:
strToday = Month(Date) & "/" & Day(Date) & "/" & Year(Date)
and use it:
RngToFilter.AutoFilter Field:=1, Operator:=xlFilterValues, Criteria1:=strToday

Or you write =Today() in G1 and then use it like this:
..AutoFilter Field:=1, Criteria1:=">=" & .Range("G1").Value2



Regards
Claus Busch
--
Win XP PRof SP2 / Vista Ultimate SP2
Office 2003 SP2 /2007 Ultimate SP2
  #10  
Old July 30th 12, 06:35 PM posted to microsoft.public.excel.programming
Ron Rosenfeld[_2_]
external usenet poster
 
Posts: 888
Default Autofilter problem

On Mon, 30 Jul 2012 05:00:42 -0700 (PDT), Ludo > wrote:

><SNIP>
>>
>>
>>
>> I'm pretty sure that the date needs to be in the format of m/d/yyyy. VBA is US-Centric
>>
>> So try strToday = format(date, "m/d/yyyy")

>
>Hi Ron,
>
>Tried it in my original code dut get still the same error.
>The sollution from Claus works pritty well.
>
>Anyhow thanks a lot for your remark.
>Working with date and time is sometimes a tricky issue.
>
>Regards,
>Ludo


It is tricky. If you want to explore further, I'd try formatting strDate so that it matches the Short Date setting in your Windows Regional settings dialog (NOT the Excel Format). I suppose that might be d/m/yyyy or "short date"

 




Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
AutoFilter Problem Dennis Excel Discussion (Misc queries) 1 October 22nd 08 05:28 AM
autofilter problem Rpeled1 Excel Programming 1 June 5th 07 05:32 PM
PROBLEM WITH AUTOFILTER OMIDF New Users to Excel 0 August 18th 06 05:36 PM
autofilter problem emre Excel Worksheet Functions 0 March 29th 05 10:19 PM
AutoFilter Problem longbow Excel Programming 3 November 24th 03 06:35 AM


All times are GMT +1. The time now is 02:59 AM.


Powered by vBulletin® Version 3.6.4
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.
Copyright ©2004-2013 ExcelBanter.
The comments are property of their posters.