VBA PivotTable PivotFields Visible Specific Other Not

Myproblem

Board Regular
Joined
May 24, 2010
Messages
198
I have VBA code under, it is working OK except in the part where is PivotFields("BRANCH_NAME"), it did not work OK.
I want to named Branches (A,B,C) to be visible, and other not visible, but my code show all branches (beside A,B,C AND other) which is wrong.
I need to modify code in the way that only specific branch name be visible
any idea, solution

ps original code: pivot crunching data 2007

Sub Branches()<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
' Page 206 of Pivot Table Data Crunching<o:p></o:p>
Dim WSD As Worksheet<o:p></o:p>
Dim PTCache As PivotCache<o:p></o:p>
Dim PT As PivotTable<o:p></o:p>
Dim PRange As Range<o:p></o:p>
Dim FinalRow As Long<o:p></o:p>
Set WSD = Worksheets("PivotTable")<o:p></o:p>
<o:p></o:p>
Application.ScreenUpdating = False<o:p></o:p>
Application.DisplayAlerts = False<o:p></o:p>
<o:p></o:p>
Worksheets.Add(After:=Worksheets("PivotTable")).Name = "Branches"<o:p></o:p>
<o:p> </o:p>
' Delete any prior pivot tables<o:p></o:p>
For Each PT In WSD.PivotTables<o:p></o:p>
PT.TableRange2.Clear<o:p></o:p>
Next PT<o:p></o:p>
WSD.Range("BA1:CA1").EntireColumn.Clear<o:p></o:p>
<o:p></o:p>
<o:p></o:p>
' Define input area and set up a Pivot Cache<o:p></o:p>
FinalRow = WSD.Cells(Application.Rows.Count, 1).End(xlUp).Row<o:p></o:p>
FinalCol = WSD.Cells(1, Application.Columns.Count). _<o:p></o:p>
End(xlToLeft).Column<o:p></o:p>
Set PRange = WSD.Cells(1, 1).Resize(FinalRow, FinalCol)<o:p></o:p>
Set PTCache = ActiveWorkbook.PivotCaches.Add(SourceType:= _<o:p></o:p>
xlDatabase, SourceData:=PRange.Address)<o:p></o:p>
<o:p></o:p>
' Create the Pivot Table from the Pivot Cache<o:p></o:p>
Set PT = PTCache.CreatePivotTable(TableDestination:=Worksheets("Branches"). _<o:p></o:p>
Cells(2, 2), TableName:="PivotTable1")<o:p></o:p>
<o:p></o:p>
' Turn off updating while building the table<o:p></o:p>
PT.ManualUpdate = False<o:p></o:p>
<o:p></o:p>
' Set up the row & column fields<o:p></o:p>
PT.AddFields RowFields:=Array("BRANCH_NAME"), _<o:p></o:p>
ColumnFields:="K_D"<o:p></o:p>
<o:p> </o:p>
' Set up the data fields<o:p></o:p>
With PT.PivotFields("AMOUNT")<o:p></o:p>
.Orientation = xlDataField<o:p></o:p>
.Function = xlSum<o:p></o:p>
.Position = 1<o:p></o:p>
.NumberFormat = "#,###"<o:p></o:p>
End With<o:p></o:p>
<o:p></o:p>
<o:p> </o:p>With PT.PivotFields("BRANCH_NAME")<o:p></o:p>
.PivotItems("BRANCH_A").Visible = True<o:p></o:p>
.PivotItems("BRANCH_B").Visible = True<o:p></o:p>
.PivotItems("BRANCH_C").Visible = True<o:p></o:p>
<o:p> </o:p>
End With<o:p></o:p>
<o:p></o:p>
' Calc the pivot table<o:p></o:p>
PT.ManualUpdate = False<o:p></o:p>
PT.ManualUpdate = False<o:p></o:p>
<o:p></o:p>
' Format the pivot table<o:p></o:p>
<o:p> </o:p>
Worksheets("Branches").Range("B3:H3").Select<o:p></o:p>
With Selection<o:p></o:p>
.HorizontalAlignment = xlCenter<o:p></o:p>
.VerticalAlignment = xlCenter<o:p></o:p>
.WrapText = True<o:p></o:p>
.ColumnWidth = 20<o:p></o:p>
<o:p> </o:p>
End With<o:p></o:p>
<o:p> </o:p>
End Sub
 

Excel Facts

Return population for a City
If you have a list of cities in A2:A100, use Data, Geography. Then =A2.Population and copy down.
Maybe you need something like (NOT TESTED)

Code:
Dim PVI as PivotItem 
 
For each PVI in PT.PivotFields("BRANCH_NAME")
    If PVI.Name <> "BRANCH_A" AND PVI.Name <> "BRANCH_B" AND PVI.Name <> "BRANCH_C" Then
        PVI.Visible = False
    End If
Next PVI

One question:
Is this correct?
' Calc the pivot table<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
PT.ManualUpdate = False<o:p></o:p>
PT.ManualUpdate = False<o:p></o:p>


Or it should be
' Calc the pivot table<o:p></o:p>
PT.ManualUpdate = False<o:p></o:p>
PT.ManualUpdate = True

HTH

M.
 
Upvote 0
Maybe you need something like (NOT TESTED)

Code:
Dim PVI as PivotItem 
 
For each PVI in PT.PivotFields("BRANCH_NAME")
    If PVI.Name <> "BRANCH_A" AND PVI.Name <> "BRANCH_B" AND PVI.Name <> "BRANCH_C" Then
        PVI.Visible = False
    End If
Next PVI

One question:
Is this correct?
' Calc the pivot table<?xml:namespace prefix = o ns = "urn:schemas-microsoft-com:office:office" /><o:p></o:p>
PT.ManualUpdate = False<o:p></o:p>
PT.ManualUpdate = False<o:p></o:p>


Or it should be
' Calc the pivot table<o:p></o:p>
PT.ManualUpdate = False<o:p></o:p>
PT.ManualUpdate = True

HTH

M.

i test it, it does not work, i even change
Dim PVI as PivotItem to Dim PVI as PIvotField, and again it does not work

any other solution?

your question is OK, i change it to be False (i know about it)
 
Upvote 0
I made a mistake...Try this

Code:
Dim PVI as PivotItem 
 
For each PVI in PT.PivotFields("BRANCH_NAME").PivotItems
    If PVI.Name <> "BRANCH_A" AND PVI.Name <> "BRANCH_B" AND PVI.Name <> "BRANCH_C" Then
        PVI.Visible = False
    End If
Next PVI
 
Upvote 0

Forum statistics

Threads
1,224,504
Messages
6,179,142
Members
452,892
Latest member
JUSTOUTOFMYREACH

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