Remove (hide) columns from the Pivot Table where a particular columns Grand total is zero

Ronanm

Board Regular
Joined
Nov 13, 2010
Messages
107
Hi

I have spent days attempting to produce some VBA code which tidies up a Pivot Table.

I want to remove (hide) columns from the Pivot Table where a particular columns Grand total is zero. So only show me the columns (these are periods) where there is a total.

My code is as below; It's the PivotTables("MyPivot").PivotFields(i).grand total = 0 bit which isn't right, and the "For Each cell"won't work either to identify the total as it's in a different loop count from the Pivotfield items... .


Code:
' first use "find" to select the Grand total cell
  Cells.Find(What:="Grand total", After:=ActiveCell, LookIn:=xlFormulas, _
       LookAt:=xlPart, SearchOrder:=xlByRows, SearchDirection:=xlNext, _
        MatchCase:=False, SearchFormat:=False).Activate

        'extend it to the PT end
        ActiveSheet.Range(ActiveCell, ActiveCell.End(xlToRight)).Select
       i = 1
       ActiveSheet.Range(ActiveCell, ActiveCell.End(xlToRight)).Select
      'now loop/hide based on cell values.
        For Each cell In Selection
        myval = cell.Value
        ppp = PivotTables("MyPivot").PivotFields(i).Name
        If [B]PivotTables("MyPivot").PivotFields(i).grand total = 0 [/B]And PivotTables("MyPivot").PivotFields(i).Name Like "*/*" Then
        ppp = PivotTables("MyPivot").PivotFields(i).Name
        
                        
                PivotTables("MyPivot").PivotFields("Sum of " & PivotTables("MyPivot").PivotFields(i).Name).Orientation = xlHidden
         
            End If
             i = i + 1
             
             
       Next cell
'Xxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxxx

Many thanks
 

Excel Facts

Show numbers in thousands?
Use a custom number format of #,##0,K. Each comma after the final 0 will divide the displayed number by another thousand

Forum statistics

Threads
1,215,514
Messages
6,125,265
Members
449,219
Latest member
daynle

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