+ Reply to Thread
Results 1 to 7 of 7

Select Pivot Slicer Item when A1 Value Changes

Hybrid View

ggmkp Select Pivot Slicer Item when... 10-18-2019, 12:59 AM
Andy Pope Re: Select Pivot Slicer Item... 10-18-2019, 03:41 AM
ggmkp Re: Select Pivot Slicer Item... 10-18-2019, 11:35 AM
Andy Pope Re: Select Pivot Slicer Item... 10-21-2019, 03:02 AM
ggmkp Re: Select Pivot Slicer Item... 10-21-2019, 03:20 PM
ggmkp Re: Select Pivot Slicer Item... 10-21-2019, 05:37 PM
ggmkp Re: Select Pivot Slicer Item... 10-21-2019, 06:31 PM
  1. #1
    Registered User
    Join Date
    05-02-2019
    Location
    USA
    MS-Off Ver
    2016
    Posts
    84

    Question Select Pivot Slicer Item when A1 Value Changes

    I'm trying to select an item in Pivot Slicer when I change "A1" value.
    So when I change the "A1" value to "ABC" the code should look for "ABC" in the Slicer Item and select it.
    "A1" value will be same as the items in the slicer.

    Here is my try, but doesn't seems to work and doesn't look right

    Private Sub WorkSheet_Change(ByVal Target As Range)
    
    Dim i As Integer
    Dim ws As Worksheet
    Set ws = ActiveSheet
    
    If Not Intersect(Target, Range("A1")) Is Nothing Then
        For i = 1 To ActiveWorkbook.SlicerCaches("Slicer_Test").SlicerItems.Count
            If ActiveWorkbook.SlicerCaches("Slicer_Test").SlicerItems(i).Value = ActiveSheet.Range("A1") Then
               ActiveWorkbook.SlicerCaches("Slicer_Test").SlicerItems(i).Selected = True
            Else
               ActiveWorkbook.SlicerCaches("Slicer_Test").SlicerItems(i).Selected = False
            End If
        Next
    End If 
    End Sub
    Thank you for help!

  2. #2
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Select Pivot Slicer Item when A1 Value Changes

    This worked for me in a very simple test.

    Private Sub Worksheet_Change(ByVal Target As Range)
    
        Dim i As Integer
        Dim ws As Worksheet
        Set ws = ActiveSheet
    
        If Not Intersect(Target, Range("A1")) Is Nothing Then
            ActiveWorkbook.SlicerCaches("Slicer_Test").Slicers(1).SlicerCache.ClearAllFilters
            For i = 1 To ActiveWorkbook.SlicerCaches("Slicer_Test").SlicerItems.Count
                If ActiveWorkbook.SlicerCaches("Slicer_Test").SlicerItems(i).Value <> ActiveSheet.Range("A1") Then
                   ActiveWorkbook.SlicerCaches("Slicer_Test").SlicerItems(i).Selected = False
                End If
            Next
        End If
    
    End Sub
    If you still need help post example workbook which illustrates the problem
    Cheers
    Andy
    www.andypope.info

  3. #3
    Registered User
    Join Date
    05-02-2019
    Location
    USA
    MS-Off Ver
    2016
    Posts
    84

    Re: Select Pivot Slicer Item when A1 Value Changes

    This works, I was missing the .ClearAllFilters
    Thank you!

    If I want to do the same for Pivot table filter instead would my code look something like this?

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim pt As PivotTable
    Dim Field As PivotField
    Dim A1Value As String
    
    If Not Intersect(Target, Range("A1")) Is Nothing Then
    
    Set pt = Worksheets("Sheet2").PivotTables("PivotTable2")
    Set Field = pt.PivotFields("Test")
    A1Value = Worksheets("Sheet1").Range("A1").Value
    
    pt.ManualUpdate = True 
    
    With pt
    Field.ClearAllFilters
    Field.CurrentPage = A1Value
    pt.RefreshTable
    End With
    
    pt.ManualUpdate = False
    
    End If
    End Sub
    Looks like I'm missing .Visible property and others
    I added the ManualUpdate because I don't want to refresh on each status change. But not sure if I did it correctly
    Last edited by ggmkp; 10-18-2019 at 11:37 AM.

  4. #4
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    O365
    Posts
    20,482

    Re: Select Pivot Slicer Item when A1 Value Changes

    Code works for me.

    If it is not doing what you expect then post example workbook to illustrate the problem

  5. #5
    Registered User
    Join Date
    05-02-2019
    Location
    USA
    MS-Off Ver
    2016
    Posts
    84

    Re: Select Pivot Slicer Item when A1 Value Changes

    I'm getting an error at "Field.CurrentPage = A1Value".

    Reads "Run-time error 1004. Unable to set CurrentPage property of PivotField class".

    This is probably because I'm reading from CurrentPage but have pivot table is in a different sheet ("Sheet2")

    And I have this code in Sheet 1
    Last edited by ggmkp; 10-21-2019 at 03:25 PM.

  6. #6
    Registered User
    Join Date
    05-02-2019
    Location
    USA
    MS-Off Ver
    2016
    Posts
    84

    Re: Select Pivot Slicer Item when A1 Value Changes

    I tried to change it to filter the Row Label instead:

    Private Sub Worksheet_Change(ByVal Target As Range)
    
    Dim pt As PivotTable
    Dim Field As PivotField
    Dim A1Value As String
    
    If Not Intersect(Target, Range("A1")) Is Nothing Then
    
    Set pt = Worksheets("Sheet2").PivotTables("PivotTable1")
    Set Field = pt.PivotFields("Test")
    A1Value = Worksheets("Sheet1").Range("A1").Value
    
    pt.ManualUpdate = True 
    
    With Field
        .ClearAllFilters
        .PivotFilters.Add Type:=xlValueEquals, Value:="A1Value"
    End With
    
    pt.RefreshTable
    pt.ManualUpdate = False
    
    End If
    End Sub
    But now I get a compile error "Named argument not found" at .PivotFilters.Add
    I tried adding quotation on "A1Value" but with or without I'm getting same error.


    I have the code in Sheet 1, and the Pivot table is in Sheet 2.

  7. #7
    Registered User
    Join Date
    05-02-2019
    Location
    USA
    MS-Off Ver
    2016
    Posts
    84

    Re: Select Pivot Slicer Item when A1 Value Changes

    I figured it out.

    It's not Type:=xlValueEquals

    It's Type:=xlCaptionEquals

+ 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. Replies: 0
    Last Post: 08-24-2017, 12:53 AM
  2. how to select slicer item from a range cell
    By maximeb in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 05-24-2017, 10:06 PM
  3. [SOLVED] Run macro to change pivot table row field when user selected a slicer item
    By Chrispelletier in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-20-2016, 09:44 AM
  4. Pivot Chart changes total when I select items from the slicer
    By Mrs.H in forum Excel Charting & Pivots
    Replies: 1
    Last Post: 02-11-2016, 01:55 PM
  5. Workbook_Open to select first item in slicer/pivot
    By kev_33 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 03-02-2015, 01:08 AM
  6. [SOLVED] Pivot Item. How to select only one.
    By mortphil in forum Excel Programming / VBA / Macros
    Replies: 8
    Last Post: 12-15-2014, 10:46 AM
  7. How to auto select from a Slicer last or top item
    By xtrenge in forum Excel Charting & Pivots
    Replies: 0
    Last Post: 01-07-2014, 06:22 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