+ Reply to Thread
Results 1 to 7 of 7

how to autofilter a pivot table?

Hybrid View

  1. #1
    Registered User
    Join Date
    07-01-2015
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    66

    Exclamation how to autofilter a pivot table?

    Basically , I have created a pivot table in a new worksheet .
    But I dont have any idea to create a data filter of the pivot table in vba code ?

    I want to filter all data with the value(Count of Last Name) = 1

    Based on macro recorder for fitler data:
    ActiveSheet.Range("$A$3:$C$122").AutoFilter Field:=2, Criteria1:="1"
    Here's my code for Create Pivot Table:
    Private Sub CreatePivot()
    
    Dim ws As Worksheet
    Dim pc As PivotCache
    Dim pt As PivotTable
    Dim objField As PivotField
    Dim rngData As Range
    Dim wsData As Worksheet 'Old ws
    Dim dataField As PivotField
    Dim lastRow As Long
    Dim lastColumn As Long
    
    'Select the sheet and first cell of the table that contains the data.
    'Set worksheet which contain the source data
    Set wsData = Worksheets("Simpat")
    
    'Delete any prior pivot tables
    For Each pt In wsData.PivotTables
        pt.TableRange2.Clear
    Next pt
        
    'determine source data range (dynamic):
    'last row in column no. 1:
    lastRow = wsData.Cells(Rows.Count, 1).End(xlUp).Row
    'last column in row no. 1:
    lastColumn = wsData.Cells(1, Columns.Count).End(xlToLeft).Column
    
    Set rngData = wsData.Cells(1, 1).Resize(lastRow, lastColumn)
    
    'Create Pivot Caches
     Set pc = ActiveWorkbook.PivotCaches.Create(SourceType:=xlDatabase, SourceData:=rngData, Version:= _
            xlPivotTableVersion15)
    
    'Creating Pivot Table
    Sheets("SimPat").Select
    Set pt = pc.CreatePivotTable(TableDestination:="", TableName:="PivotTable1", DefaultVersion:=xlPivotTableVersion15)
    
    'Turn off automatic updation of Pivot Table during the process of its creation to speed up code.
    pt.ManualUpdate = True
    
    'Setting fields
    Set objField = pt.PivotFields("Last Name")
        objField.Orientation = xlRowField
        objField.Position = 1
        
    'Set Data Field
      With pt.PivotFields("Last Name")
      .Orientation = xlDataField
      .Function = xlCount
      .Position = 1
      End With
              
      With ws
      .AutoFilterMode = False
      .Range("A1").AutoFilter
      .Range("A1").AutoFilter Field:=2, Criteria1:="1"
      ' .Range(wsData.Cells(1, 1).Resize(lastRow, lastColumn)).AutoFilter Field:=2, Criteria1:="1"
      End With
      
    'Turn on automatic update / calculation in the Pivot Table
    pt.ManualUpdate = False
    
    End Sub

    Attachment 407580

    This what will happens , after filtering the data to be "1"
    Then , I would like to make the "Row Labels" or "Name" to be same
    regardless if the name has spacing or capital letters. Just want to make it the same. How do i do this too ?

    Please help

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: how to autofilter a pivot table?

    attach a worksheet
    cannot work with an image
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Registered User
    Join Date
    07-01-2015
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    66

    Exclamation Re: how to autofilter a pivot table?

    Quote Originally Posted by humdingaling View Post
    attach a worksheet
    cannot work with an image
    What do you mean ?
    Attached Files Attached Files
    Last edited by fluffyvampirekitten; 07-20-2015 at 01:51 AM.

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: how to autofilter a pivot table?

    attachment is a jpg...cannot work with jpg

  5. #5
    Registered User
    Join Date
    07-01-2015
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    66

    Re: how to autofilter a pivot table?

    I attached the sample workbook in the previous post

  6. #6
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: how to autofilter a pivot table?

    you cant autofilter a pivot table

    replace
      With ws
      .AutoFilterMode = False
      .Range("A1").AutoFilter
      .Range("A1").AutoFilter Field:=2, Criteria1:="1"
      ' .Range(wsData.Cells(1, 1).Resize(lastRow, lastColumn)).AutoFilter Field:=2, Criteria1:="1"
      End With
    with
        ActiveSheet.PivotTables("PivotTable1").PivotFields("Last Name").PivotFilters. _
            Add Type:=xlValueEquals, dataField:=ActiveSheet.PivotTables("PivotTable1"). _
            PivotFields("Count of Last Name"), Value1:=1

  7. #7
    Registered User
    Join Date
    07-01-2015
    Location
    Singapore
    MS-Off Ver
    2013
    Posts
    66

    Re: how to autofilter a pivot table?

    Quote Originally Posted by humdingaling View Post
    you cant autofilter a pivot table

    replace
      With ws
      .AutoFilterMode = False
      .Range("A1").AutoFilter
      .Range("A1").AutoFilter Field:=2, Criteria1:="1"
      ' .Range(wsData.Cells(1, 1).Resize(lastRow, lastColumn)).AutoFilter Field:=2, Criteria1:="1"
      End With
    with
        ActiveSheet.PivotTables("PivotTable1").PivotFields("Last Name").PivotFilters. _
            Add Type:=xlValueEquals, dataField:=ActiveSheet.PivotTables("PivotTable1"). _
            PivotFields("Count of Last Name"), Value1:=1


    OMG! It can work. Thank You T^T

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. Always show seach bar in pivot table/autofilter...
    By Ben1985 in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 11-18-2013, 05:14 PM
  2. Macro to autofilter pivot table for last 60 days
    By oldmanexcel in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-12-2013, 03:06 AM
  3. Macro Help Needed - Link Pivot Table Filter to Regular Table AutoFilter
    By ycx1129 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 11-11-2011, 12:54 PM
  4. PIVOT table autofilter based on data validation cell selection
    By fredstyler in forum Excel Formulas & Functions
    Replies: 5
    Last Post: 02-16-2011, 09:23 AM
  5. Pivot Table with AutoFilter
    By raggatip in forum Excel General
    Replies: 1
    Last Post: 01-16-2011, 07:35 PM
  6. How to autofilter a pivot table
    By jordan.lawrance in forum Tips and Tutorials
    Replies: 0
    Last Post: 05-20-2009, 06:56 AM
  7. [SOLVED] Can Pivot table drop-downs act like AutoFilter
    By RickJ7777 in forum Excel General
    Replies: 1
    Last Post: 05-19-2006, 07:55 PM
  8. [SOLVED] VBA to access Pivot Table Autofilter dropdown box
    By Dennis in forum Excel General
    Replies: 1
    Last Post: 03-07-2006, 04:30 PM

Tags for this Thread

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