+ Reply to Thread
Results 1 to 2 of 2

Cell text as pivottable filter

Hybrid View

  1. #1
    Registered User
    Join Date
    10-12-2011
    Location
    Denmark
    MS-Off Ver
    Excel 2007
    Posts
    1

    Cell text as pivottable filter

    Hi People,

    (cross posted from another forum: http://www.vbaexpress.com/forum/showthread.php?t=39403 )

    I hope you can help me with a little problem.

    I have a script (please see below) which uses cell text to filter a pivottable ("Pivottable2") on two pivotfields. The two pivotfields ("Region","Name") are filtered by the named cells ("RegionFilterRange","RegionFilterRange2").

    If I filter the pivottable in one of the following two ways, it works fine:
    1) enter a text in "RegionFilterRange"
    OR
    1) enter a text in "RegionFilterRange"
    2) enter a text in "RegionFilterRange2"

    BUT,

    If I do it backwards:
    1) enter a text in "RegionFilterRange2"
    OR
    1) enter a text in "RegionFilterRange2"
    2) enter a text in "RegionFilterRange"

    Nothing happens ?

    What I am aiming at, is to enter a text in one cell or/and the other, not in a particular order ?

    Also, If I clear the "RegionFilterRange"-cell, the pivottable clears the filter,
    BUT if I clear the "RegionFilterRange2"-cell, nothing happens ?

    any ideas ? Please advice.

    Br,
    J

    P.S. I have attached a spreadsheet with an example.




    Public Sub UpdatePivotFieldFromRange( _ 
        ByVal RangeName As String, _ 
        ByVal FieldName As String, _ 
        ByVal PivotTableName As String) 
         
        Dim Sheet As Worksheet 
        Dim pt As PivotTable 
        Dim rng1, rng2 As Range 
        Dim vecItems As Variant 
         
        Set rng1 = Application.Range("RegionFilterRange") 
        Set rng2 = Application.Range("RegionFilterRange2") 
         
        For Each Sheet In Application.ActiveWorkbook.Worksheets 
            On Error Resume Next 
            Set pt = Sheet.PivotTables("PivotTable2") 
        Next 
         
        On Error Goto Ex 
         
        If Not pt Is Nothing Then 
             
            pt.ManualUpdate = True 
            Application.EnableEvents = False 
            Application.ScreenUpdating = False 
             
            Dim Field As PivotField, Field2 As PivotField 
             
            Set Field = pt.PivotFields("Region") 
            Set Field2 = pt.PivotFields("Name") 
             
            Field.ClearAllFilters 
            Field.EnableItemSelection = False 
             
            If Range(rng1, rng2).Text = "(All)" Then 
                Call ResetAllItems(pt, Range("Region", "Name")) 
                 
            Else 
                 
                vecItems2 = GetItems(Worksheets("Sheet1").Range("B2:B20"), rng2.Text) 
                Call SelectPivotItem(Field2, vecItems2) 
                vecItems1 = GetItems(Worksheets("Sheet1").Range("A2:A20"), rng1.Text) 
                Call SelectPivotItem(Field, vecItems1) 
                 
                 
                 
            End If 
             
            pt.RefreshTable 
             
        End If 
         
         
    Ex: 
        pt.ManualUpdate = False 
        Application.EnableEvents = True 
        Application.ScreenUpdating = True 
    End Sub
    Attached Files Attached Files
    Last edited by JayJay6; 10-14-2011 at 08:29 AM.

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

    Re: Cell text as pivottable filter

    Welcome to the forum.

    However:

    Your post does not comply with Rule 8 of our Forum RULES. Cross-posting is when you post the same question in other forums on the web. You'll find people are disinclined to respond to cross-posts because they may be wasting their time solving a problem that has been solved elsewhere. We prefer that you not cross-post at all, but if you do (and it's unlikely to go unnoticed), you MUST provide a link (copy the url from the address bar in your browser)to the cross-post. Expect cross-posts without a link to be closed a message will be posted by the moderator explaining why. We are here to help so help us help you!

    Read this to understand why we ask you to do this, and then please edit your post to add links to any and all crossposts in other forums.
    Everyone who confuses correlation and causation ends up dead.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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