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