Filter Pivot Table With VBA

mouse88

Board Regular
Joined
May 24, 2011
Messages
148
I have previously used the following code to filter a pivot table by date range:

Code:
For Each PivItem In PivTable.PivotFields("Date").PivotItems
    If PivItem.Value < DateFrom Or PivItem.Value > DateTo Then
        If PivItem.Visible = True Then
            PivItem.Visible = False
        End If
    End If
Next PivItem

It worked fine yesterday but today in another workbook I get a data type mismatch error of the line:

Code:
If PivItem.Visible = True Then

Any Ideas?
 

Excel Facts

What do {} around a formula in the formula bar mean?
{Formula} means the formula was entered using Ctrl+Shift+Enter signifying an old-style array formula.
For anyone with this problem which I believe is only in Excel 2010 I used the following line of code to fix it:

Code:
PivTable.PivotFields("Date").NumberFormat = "dd/mm/yyyy"

It seems that Excel formats the date in a different way when retreiving the data from the pivot item. In my pivot table it would be formatted as "01/01/2011" but Excel would return the value of "1/1/2011" without the leading zeros.

Problem is this solution has to work in Excel 2007 so im not sure if that has the same problem :confused:
 
Upvote 0

Forum statistics

Threads
1,224,538
Messages
6,179,412
Members
452,912
Latest member
alicemil

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top