Programming the filter of a pivot table

G

Guest

I just can't get through this ... I've spent hours on it & I'm sure it's very
simple !!

I download a (.txt) file with tons dates of events in it. Once this is done,
i run a macro that does numerous things, including filter the list & then
refresh the pivot table. Info that have been filtered in the list still
appear in the pivot table. How can I get rid of them ? (I join the part of
the code where I filter & refresh)

'filter the list to get the next 4 months
Selection.AutoFilter Field:=9, Criteria1:=">=today()", Operator:=xlAnd _
, Criteria2:="<=TODAY()+122"
'refresh the pivottable
Sheets("Dynamique").Select
ActiveSheet.PivotTables("Tableau croisé dynamique2").PivotCache.Refresh

Please help me !!
 
B

Bernie Deitrick

This works for me:

Selection.AutoFilter Field:=9, Criteria1:=">=" & Now(), Operator:=xlAnd, _
Criteria2:="<=" & Now() + 122
 
G

Guest

The problem isn't to filter the list. Both your code & mine works fine. But
either way, when I update the pivot table related to that list, filtered &
unfiltered lines all show into the pivot table. I only want the ones in the
right date range.
 
B

Bernie Deitrick

JEan-Marc,

Sorry for misunderstanding your question.

Insert another column into your database, and use a formula like

=AND(I2>=TODAY(),I2<=DATE(YEAR(TODAY()),MONTH(TODAY()) +4,DAY(TODAY())))

(this is assuming that your dates are in column I)

Then use that column (which will return TRUE and FALSE) as either a row or page filter, and select
only TRUE.

In code, you could do that like this:

Range("J1").Value = "Include"
Range("J2:J" & Cells(Rows.Count, 9).End(xlUp).Row).FormulaR1C1 = _
"=AND(RC[-1]>=TODAY(),RC[-1]<=DATE(YEAR(TODAY()),MONTH(TODAY()) +4,DAY(TODAY())))"

which will put that formula into column J, matching the entries in column I.

HTH,
Bernie
MS Excel MVP
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top