ISO Help: Pivot Tables & VBA coding....

JDM-LTCS

New Member
Joined
Sep 7, 2006
Messages
40
Thanks in advance, BTW! :)

----------------------------------------

Here's the code I'm trying to run:

WalkDate = Worksheets("Print Me!").Range("DateChoice").Text
' WalkDate is cell F3
'

ActiveSheet.PivotTables("PivotTable1").PivotFields("Date").PivotItems("Show All").Visible = False

Worksheets("Print Me!").PivotTables("PivotTable1").PivotFields("Date").PivotItems(WalkDate).Visible = True



What I'm wanting is for the Pivot Table to filter by no date fields and then filter only by the field set in cell F3 (WalkDate).


Does this make sense? I'm getting an error message stating "Run-time error '1004': Unable to get the PivotItems property of the PivotField class"


:oops:
 

Excel Facts

Repeat Last Command
Pressing F4 adds dollar signs when editing a formula. When not editing, F4 repeats last command.

pgc01

MrExcel MVP
Joined
Apr 25, 2006
Messages
19,884
Hi JDM-LTCS

I cannot test your code it because I have xl2000 and "Show all" was introduced in xl2002, but ...

... I'm convinced that the rule that says that it's not allowed to hide all the items in a field is still valid in xl 2002/2003. At one item must be visible. So I think that

... .PivotItems("Show All").Visible = False

is not allowed.

You can make the WalkDate field visible and the hide all others.
Something like:

Code:
dim PI As PivotItem

...

With Worksheets("Print Me!").PivotTables("PivotTable1").PivotFields("Date")
    .PivotItems(WalkDate).Visible = True
    For Each PI In .PivotItems
        If PI.Name <> WalkDate Then PI.Visible = False
    Next
End With

Please test it.
PGC

EDIT: added declaration of PI
 

JDM-LTCS

New Member
Joined
Sep 7, 2006
Messages
40
This is the code I came up with. It seems to work like a charm. I'll plug your code in place of mine and see how it works when I get a free moment.


Thanks!! :)


-------------------------------


Dim pt As PivotTable
Dim pf As PivotField
Dim pi As PivotItem
Dim WalkDate As Date


On Error Resume Next
For Each pt In ActiveSheet.PivotTables
For Each pf In pt.ColumnFields
pf.AutoSort xlManual, pf.SourceName
For Each pi In pf.PivotItems
If pi = WalkDate Then
pi.Visible = True
Else
pt.PivotFields("Date").PivotItems("(blank)").Visible = False
pi.Visible = False
End If
Next
Next
pf.AutoSort xlAscending, pf.SourceName
Next
 

Forum statistics

Threads
1,136,619
Messages
5,676,847
Members
419,655
Latest member
pd2021vb

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
Top