Pivot table manipulation VBA code

rajaniesh

New Member
Joined
Aug 13, 2013
Messages
48
Hi,

I have a pivot table in a sheet. It has hours into it. I want to select All hours and then uncheck zero and blank hours. I am using the below vba code but it does nto work as expected.


Sub FilterPivotHours()
currentWorkBookName = ThisWorkbook.Name
Workbooks(currentWorkBookName).Sheets("Pivot").Activate
ActiveSheet.PivotTables("PivotTable1").PivotFields("Hours"). _
CurrentPage = "(All)"
With ActiveSheet.PivotTables("PivotTable1").PivotFields("Hours")
On Error Resume Next
.PivotItems("(All)").Visible = True
.PivotItems("0").Visible = False
.PivotItems("(blank)").Visible = False
On Error GoTo 0
End With
End Sub

Regards
Rajaniesh
 

Excel Facts

When they said...
When they said you are going to "Excel at life", they meant you "will be doing Excel your whole life".
Code:
Sub FilterPivotHours()
  Dim pvt As PivotTable
  Dim pvf As PivotField
  
  Set pvt = ThisWorkbook.Worksheets("Pivot").PivotTables("PivotTable1")
  Set pvf = pvt.PivotFields("Hours")
  pvf.ClearAllFilters
  
  On Error Resume Next
  pvf.PivotItems("(blank)").Visible = False
  pvf.PivotItems("0").Visible = False
End Sub
 
Upvote 0
What we need to change in the code if we need to modify the code so that we can only select two values AD and Ba but deselect all other values in the below code


Sub FilterPivotAccountFilter()
Dim pvt As pivotTable
Dim pvf As PivotField

Set pvt = ThisWorkbook.Worksheets("Pivot").PivotTables("PivotTable1")
Set pvf = pvt.PivotFields("Account Filter")
'pvf.ClearAllFilters

On Error Resume Next

pvf.PivotItems("Ba").Visible = True
pvf.PivotItems("AD").Visible = True
pvf.PivotItems("(All)").Visible = False

End Sub
 
Upvote 0

Forum statistics

Threads
1,214,561
Messages
6,120,242
Members
448,951
Latest member
jennlynn

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