I am trying to filter a Total_Effect field in a pivot table based on two filters. The first filter is for value less than 0% and this result is then filtered to give me the bottom five results. The following shows how the source data could look like (note I have simplified the source data for this example). In the first example, it should give me the value of -0.20%, -0.80% & -1.00% and the second example should produce value of -0.15%, -0.20%, -0.40%, -0.80% & -1.00%.
Stock Total_Effect
A 0.30%
B -0.20%
C -1.00%
D -0.80%
E 1.10%
Stock Total_Effect
A 0.30%
B -0.20%
C -1.00%
D -0.80%
E 1.10%
F -0.40%
G -0.15%
I know the vba code for filtering value less than 0 (shown below in bold) but don't know how to add another filter on the result of the first filter.
Sub createPivotTable()
Dim PvtTbl As PivotTable
Dim wsData As Worksheet
Dim rngData As Range
Dim PvtTblCache As PivotCache
Dim wsPvtTbl As Worksheet
Dim pvtFld As PivotField
Dim rngChart As Range
Dim objChart As ChartObject
Dim n As Long, lastCol As Long, lastRow As Long
Dim ItemName As String
Dim StartCell As Range
'determine the worksheet which contains the source data
Set wsData = Sheet1
'determine the worksheet where the new PivotTable will be created
Set wsPvtTbl = Worksheets("Sheet2")
'delete all existing Pivot Tables in the worksheet
For Each PvtTbl In wsPvtTbl.PivotTables
PvtTbl.TableRange2.Clear
Next PvtTbl
'delete pivot chart
For Each objChart In wsPvtTbl.ChartObjects
objChart.Delete
Next objChart
'Create a range data for PT
Set StartCell = wsData.Range("A1")
lastRow = Range(wsData.Cells(1, 1), wsData.Cells(1, 1).End(xlDown)).Rows.Count
lastCol = Range(wsData.Cells(1, 1), wsData.Cells(1, 1).End(xlToRight)).Columns.Count
Set rngData = wsData.Range(StartCell, wsData.Cells(lastRow, lastCol))
'Create a new PivotTable cache, and then create a new PivotTable report based on the cache.
Set PvtTblCache = ActiveWorkbook.PivotCaches.Create( _
SourceType:=xlDatabase, _
SourceData:=rngData, _
Version:=xlPivotTableVersion14)
For n = 1 To 4
ItemName = wsData.Cells(1, n + 8)
Set PvtTbl = wsPvtTbl.PivotTables.Add( _
PivotCache:=PvtTblCache, _
TableDestination:=wsPvtTbl.Cells(32, 6 * (n - 1) + 1), _
DefaultVersion:=xlPivotTableVersion14)
'Turn off automatic update of Pivot Table during the process of its creation to speed up code.
PvtTbl.ManualUpdate = True
'add row, column and page (report filter) fields:
Set pvtFld = PvtTbl.PivotFields(ItemName)
pvtFld.Orientation = xlPageField
pvtFld.CurrentPage = "TRUE"
Set pvtFld = PvtTbl.PivotFields("Stock")
pvtFld.Orientation = xlRowField
'set data field - specifically change orientation to a data field and set its function property:
With PvtTbl.PivotFields("Total_Effect")
.Orientation = xlDataField
.Function = xlSum
.NumberFormat = "0.00%"
.Position = 1
End With
PvtTbl.ColumnGrand = False
PvtTbl.PivotFields("Stock").PivotFilters.Add Type:=xlValueIsLessThan, DataField:=PvtTbl.PivotFields("Sum of Total_Effect"), Value1:=0
' PvtTbl.PivotFields("Stock").PivotFilters.Add Type:=xlBottomCount, DataField:=PvtTbl.PivotFields("Sum of Total_Effect"), Value1:=5
PvtTbl.PivotFields("Stock").AutoSort Order:=xlDescending, Field:="Sum of Total_Effect"
'turn on automatic update / calculation in the Pivot Table
PvtTbl.ManualUpdate = False
Next n
End Sub
Thanks in advance
Bookmarks