+ Reply to Thread
Results 1 to 6 of 6

Add Pivot Table Filters to current UserForm code.

Hybrid View

  1. #1
    Registered User
    Join Date
    04-08-2014
    Location
    usa
    MS-Off Ver
    Excel 2011
    Posts
    52

    Add Pivot Table Filters to current UserForm code.

    Hi,

    I am trying to utilize my current code to also add a filter to a Pivot Table field. Currently the code filters the worksheet, but now I need to filter the pivot table to provide data analysis through charts/graphs.

    I need to be able to do both, filter worksheet and filter pivot tables.

    I have uploaded an example version and screen shot to give an idea.

    I appreciate all the help in advance. I've made so much progress thanks to many of the forum contributors.

    Thanks!
    Attached Images Attached Images
    Attached Files Attached Files
    Last edited by Darkenergyz; 04-16-2014 at 03:31 PM.

  2. #2
    Registered User
    Join Date
    04-08-2014
    Location
    usa
    MS-Off Ver
    Excel 2011
    Posts
    52

    Re: Add Pivot Table Filters to current UserForm code.

    This is what I have now.

    Can someone tell me what I am doing wrong?

    Private Sub cmdFilter_Click()
        Dim cChk As Control, arrVals, i As Long
        Dim X, objDict As Scripting.Dictionary, lngRow As Long
    
        Application.ScreenUpdating = False
    
        'Clear filters
        On Error Resume Next
        ActiveSheet.ShowAllData
        On Error GoTo 0
    
        'Get unique list of values in the filter column (Col. "B")
        Set objDict = New Scripting.Dictionary
        X = Application.Transpose(Range("B13", Cells(Rows.Count, "B").End(xlUp)))
        For lngRow = 1 To UBound(X, 1)
            objDict(X(lngRow)) = 1
        Next lngRow
    
        For Each cChk In Me.grpFilter.Controls
            If TypeName(cChk) = "CheckBox" Then
                If cChk.Value = True Then
                    objDict.Remove cChk.Tag
                End If
            End If
        Next cChk
    
        ReDim arrVals(objDict.Count - 1)
        For i = 0 To objDict.Count - 1
            arrVals(i) = objDict.Keys(i)
        Next i
        ReDim Preserve arrVals(1 To UBound(arrVals) + 1)
        arrVals(UBound(arrVals)) = "="
        On Error Resume Next
        'Pivot Table
        ActiveSheet.PivotTables("PivotTable1").PivotFields ("Fund"), _
        varItemList:=arrVals.Visible = False
        'Worksheet
        ActiveSheet.Range("A12").CurrentRegion.AutoFilter _
            Field:=2, Criteria1:=arrVals, Operator:=xlFilterValues
        Unload Me
        Application.ScreenUpdating = False
    End Sub

    'Pivot Table
        ActiveSheet.PivotTables("PivotTable1").PivotFields ("Fund"), _
        varItemList:=arrVals.Visible = False
        'Worksheet
        ActiveSheet.Range("A12").CurrentRegion.AutoFilter _
            Field:=2, Criteria1:=arrVals, Operator:=xlFilterValues

  3. #3
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Add Pivot Table Filters to current UserForm code.

    Hi,

    I really struggled with this one, until I was informed that one cannot use an array within a Pivot Filter!!
    TX to HansV at Eileen's Lounge, I have the code for you... (http://www.eileenslounge.com/portal.php)
    Attached is the workbook file with functional sample.
    Attached Files Attached Files
    Regards,
    Rudi

  4. #4
    Forum Expert romperstomper's Avatar
    Join Date
    08-13-2008
    Location
    England
    MS-Off Ver
    365, varying versions/builds
    Posts
    22,064

    Re: Add Pivot Table Filters to current UserForm code.

    I wondered if you were the same Rudi.
    Everyone who confuses correlation and causation ends up dead.

  5. #5
    Valued Forum Contributor
    Join Date
    07-04-2012
    Location
    Cape Town, RSA
    MS-Off Ver
    Office 365 ProPlus
    Posts
    1,050

    Re: Add Pivot Table Filters to current UserForm code.

    Hi Rory,
    The one and only

  6. #6
    Registered User
    Join Date
    04-08-2014
    Location
    usa
    MS-Off Ver
    Excel 2011
    Posts
    52

    Re: Add Pivot Table Filters to current UserForm code.

    Looks good to me! I read somewhere you could use an array, but I guess that explains why I couldn't get it to work. Regardless, it looks great. Thanks.


    Now I just have to figure out how to get it to work with multiple pivot-tables/columns if possible. If it isn't possible, this is great and fulfils the requirement.

    V/r,

    Josh

+ 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. Using two filters on a Pivot table
    By huckzors in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 04-22-2013, 03:59 PM
  2. Replies: 1
    Last Post: 02-22-2012, 12:21 PM
  3. Change pivot table filters on VBA userform combox box change?
    By Teemu in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 08-16-2011, 05:49 AM
  4. VBA for Pivot Table Filters
    By Coldsteel in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 10-28-2010, 09:03 AM
  5. VBA Code for a pivot table to open database and to reference table current page
    By Pete Straman Straman via OfficeKB.com in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 02-21-2005, 12:06 AM

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