+ Reply to Thread
Results 1 to 3 of 3

MACRO - Pivot filter based on cell reference

  1. #1
    Registered User
    Join Date
    05-27-2014
    Posts
    2

    Lightbulb MACRO - Pivot filter based on cell reference

    Hi all,

    I'm currently in a new 'phase' of my excel career: connecting a cell(variable) with a pivot table filter using a macro.
    I've been looking around for quite a while and testing a few macro's but nothing worked out yet.. I would be great if you guy's could help me out!

    Could someone help me to write the right macro to filter a pivot based on 3 criteria's? (attachment for more detail)

    Macro to change one pivot filter based on datainput from another cell

    Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Intersect(Target, Range("D4")) Is Nothing Then Exit Sub

    Dim pt As PivotTable
    Dim Field As PivotField
    Dim NewCat As String

    Set pt = Worksheets("Pivots").PivotTables("PivotTable9")
    Set Field = pt.PivotFields("Ship To Postal Code")
    NewCat = Worksheets("Pivots").Range("D4").Value

    Field.ClearAllFilters
    Field.CurrentPage = NewCat
    pt.RefreshTable
    End With

    End Sub


    Thanks you so much in advance!

    Cheers

    DavidPivot filters.xlsx

  2. #2
    Forum Contributor
    Join Date
    08-16-2012
    Location
    Sydney
    MS-Off Ver
    Excel 2019
    Posts
    209

    Re: MACRO - Pivot filter based on cell reference

    Hi Maikoz,

    This is the start of Pivot table - your might need to tweek stuff here and there...
    Formula: copy to clipboard
    Please Login or Register  to view this content.


    hope this helps

    Cheers
    Lex

  3. #3
    Registered User
    Join Date
    05-27-2014
    Posts
    2

    Re: MACRO - Pivot filter based on cell reference

    Hi Lex thanks man for helping me!

    Private Sub Worksheet_SelectionChange(ByVal Target As Range) <- Error here!

    If Intersect(Target, Range("D4")) Is Nothing Then Exit Sub

    Dim PT01 As PivotTable
    Dim PTCache As PivotCache
    Dim FinalRow As Long
    Dim FinalCol As Long
    Dim PRange As Range

    Sheets("Pivots").Select 'Your Data Tab and make sure that your header is in row 1

    FinalRow = Cells(Rows.Count, 1).End(xlUp).Row
    FinalCol = Cells(1, Columns.Count).End(xlToLeft).Column
    Set PRange = Cells(1, 1).Resize(FinalRow, FinalCol)

    Set PTCache = ActiveWorkbook.PivotCaches.Add(xlDatabase, PRange)
    Sheets("Pivots").Select 'Your Summary tab
    Set PT01 = ActiveSheet.PivotTables.Add(PivotCache:=PTCache, tabledestination:=Range("A3"))
    With PT01
    .PivotFields("Billing Date.Year").Orientation = xlPageField
    .PivotFields("Ship To Address").Orientation = xlPageField
    .PivotFields("Ship To Postal Code").Orientation = xlPageField
    .PivotFields("Your Row Name").Orientation = xlRowField
    End With

    With PT01.PivotFields("What you calculate")
    .Orientation = xlDataField
    .Function = xlSum
    .NumberFormat = "0.00_ ;[Red]-0.00 "
    End With

    What i forgot to tell you that i don't have a data sheet... Pivot table is based on an OLAP query.. Is there a workaround?

    Thanks again!
    Maik

+ 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. [SOLVED] Excel Macro: Filter all pivot tables in workbook from a source(reference)
    By moxman in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 10-10-2013, 09:46 PM
  2. Pivot - Selecting filter based on a cell value ( not using Macro)
    By kumthekarg in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 04-23-2013, 02:40 PM
  3. Filter multiple pivot tables based on same cell value (OLAP based)
    By natsuki-hime in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 06-15-2012, 05:21 AM
  4. Change Pivot table Filter Based on Cell Value *Multiple Filter items* Possible?
    By Flydd in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-27-2012, 06:57 AM
  5. Problem with Macro to filter pivot based on a cell value
    By Scarface in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-06-2011, 07:18 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