Results 1 to 7 of 7

How to filter a field in pivot table using two filters

Threaded View

  1. #1
    Registered User
    Join Date
    08-25-2015
    Location
    Auckland, New Zealand
    MS-Off Ver
    2013
    Posts
    3

    How to filter a field in pivot table using two filters

    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
    Last edited by FDibbins; 08-25-2015 at 11:06 PM.

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. Replies: 1
    Last Post: 07-16-2015, 05:46 AM
  2. Replies: 0
    Last Post: 03-20-2015, 11:39 AM
  3. Applying Multiple Filters to a row field in Pivot table using VBA
    By pdalal in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-10-2015, 02:11 PM
  4. Pivot Table: Multiple Value filters on one field
    By jamsta1972 in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 03-27-2014, 02:20 PM
  5. Locking only one page field (report filters) of a pivot table in a protected sheet???
    By kzahariev in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 06-12-2013, 08:48 AM
  6. [SOLVED] How to set a 'Date Filter' on a field in the 'Report Filter' section of a Pivot Table?
    By Rhino_dance in forum Excel Charting & Pivots
    Replies: 7
    Last Post: 03-28-2013, 11:01 AM
  7. Controlling multiple pivot table filters with one filter.
    By adoepker in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 06-28-2011, 03:41 PM

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1