+ Reply to Thread
Results 1 to 6 of 6

Update a pivot chart from a slicer change to a table

Hybrid View

  1. #1
    Forum Contributor
    Join Date
    03-23-2012
    Location
    Michigan
    MS-Off Ver
    MS365 Excel version 2301 (Build 16026.20238)
    Posts
    105

    Update a pivot chart from a slicer change to a table

    Hi. I've got a data table (Table1) and a slicer. On another sheet, I have a pivot table and chart. I'd like to be able to trigger the pivot chart update when the slicer gets changed. I've tried the code below (which works as a standalone module) in a worksheet_change event, and that didn't work. From reviewing a plethora of posts, it appears that a slicer change can't trigger this event. So I tried adding a sub-total formula to cell $Z$1 on the worksheet and tried the Worksheet_SelectionChange event, and that also didn't work. I can sort of get my head around why the first one didn't work, but why wouldn't the Worksheet_SelectionChange event see the change in cell Z1? Is there any way to get this to trigger the event? Thanks for looking.

        
        Dim wkb As Workbook
        Dim wksData As Worksheet
        Dim wksPT As Worksheet
        Dim cCountry As String
        Dim FirstVizCell As String
        Dim pt As PivotTable
        Dim Field As PivotField
    
        Set wkb = ThisWorkbook
        Set wksData = wkb.Sheets("OUS Complaints")
        Set wksPT = wkb.Sheets("Escalation Pivot Tables & Chart")
    
        Set pt = wksPT.PivotTables("PivotTable1")
        Set Field = pt.PivotFields("Country")
        
        If Target.Address = "$Z$1" Then
        
            wkb.Activate
            wksData.Select
            With wksData
                FirstVizCell = Range("D3:D" & Rows.Count).SpecialCells(xlVisible)(1).Address
                cCountry = Range(FirstVizCell).Value
                'This updates and refreshes the PIVOT table
                With pt
                    Field.ClearAllFilters
                    Field.CurrentPage = cCountry
                    pt.RefreshTable
                End With
            End With
    
        End If
    
    End Sub

  2. #2
    Forum Contributor frabulator's Avatar
    Join Date
    09-16-2015
    Location
    United States
    MS-Off Ver
    2019
    Posts
    105

    Re: Update a pivot chart from a slicer change to a table

    have you tried saving the workbook and reopening it? Sometimes the workbook/worksheet triggers do not load after they have been changed unless they are present when the workbook first opens.

  3. #3
    Forum Contributor
    Join Date
    03-23-2012
    Location
    Michigan
    MS-Off Ver
    MS365 Excel version 2301 (Build 16026.20238)
    Posts
    105

    Re: Update a pivot chart from a slicer change to a table

    Yes, I tried that. Both events trigger if I update cell Z1 manually, but not if it gets updated via the Slicer change.

  4. #4
    Forum Contributor frabulator's Avatar
    Join Date
    09-16-2015
    Location
    United States
    MS-Off Ver
    2019
    Posts
    105

    Re: Update a pivot chart from a slicer change to a table

    Try to use the following trigger. I am not familiar enough with pivot charts to say if it works exactly like I think it should, but I think this is the trigger that is fired when a pivot chart/slicer is changed or updated.

    Code pulled from here.

    Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    
    If ActiveWorkbook.SlicerCaches("Slicer_Region").SlicerItems("East").Selected = True Then
    UserForm1.Show
    ElseIf ActiveWorkbook.SlicerCaches("Slicer_Region").SlicerItems("West").Selected = True Then
    UserForm2.Show
    End If
    
    End Sub

    If you are wanting the macro to fire with the slicer, you could do a workaround that says when the slider is moved have Z1 become selected. Once Z1 is selected, your chart should update, then select the previous selected cell. If you contain this inside of a Application.ScreenUpdating = False then the user will never see it.

    However, there are other declarations I found, just looking around inside of the Worksheet VBA section. You could try your hand with some of these and see what happens when?

    Private Sub Workbook_PivotTableOpenConnection(ByVal Target As PivotTable)
    
    End Sub
    
    Private Sub Workbook_SheetPivotTableAfterValueChange(ByVal Sh As Object, ByVal TargetPivotTable As PivotTable, ByVal TargetRange As Range)
    
    End Sub
    
    Private Sub Workbook_SheetPivotTableBeforeAllocateChanges(ByVal Sh As Object, ByVal TargetPivotTable As PivotTable, ByVal ValueChangeStart As Long, ByVal ValueChangeEnd As Long, Cancel As Boolean)
    
    End Sub
    
    Private Sub Workbook_SheetPivotTableBeforeCommitChanges(ByVal Sh As Object, ByVal TargetPivotTable As PivotTable, ByVal ValueChangeStart As Long, ByVal ValueChangeEnd As Long, Cancel As Boolean)
    
    End Sub
    
    Private Sub Workbook_SheetPivotTableChangeSync(ByVal Sh As Object, ByVal Target As PivotTable)
    
    End Sub
    
    Private Sub Workbook_SheetPivotTableUpdate(ByVal Sh As Object, ByVal Target As PivotTable)
    
    End Sub

  5. #5
    Forum Contributor frabulator's Avatar
    Join Date
    09-16-2015
    Location
    United States
    MS-Off Ver
    2019
    Posts
    105

    Re: Update a pivot chart from a slicer change to a table

    After sleeping on it I think I understand now. You want the chart to update when the value changes in cell Z1, correct?

    Currently, your code only checks if the cell Z1 is selected, THEN it updates the cell. Selecting Z1 and changing the value are two different things.

    To check to see if a value has changed, you can set the value to a public string. If that string matches the previous string, then do nothing. If it does not, then update.

    Here is an example:

    
    Public PreviousVal As String
    
    Private Sub Workbook_SheetSelectionChange(ByVal Sh As Object, ByVal Target As Range)
    
        
    
        Dim wkb As Workbook
        Dim wksData As Worksheet
        Dim wksPT As Worksheet
        Dim cCountry As String
        Dim FirstVizCell As String
        Dim pt As PivotTable
        Dim Field As PivotField
    
        Set wkb = ThisWorkbook
        Set wksData = wkb.Sheets(1)
        Set wksPT = wkb.Sheets(2)
    
        'Set pt = wksPT.PivotTables("PivotTable1")
        'Set Field = pt.PivotFields("Country")
        
        'If Target.Address = "$Z$1" Then
        
        If PreviousVal = "" Or PreviousVal <> Target.Value Then
        
        
            wkb.Activate
            wksData.Select
            With wksData
                FirstVizCell = Range("D3:D" & Rows.Count).SpecialCells(xlVisible)(1).Address
                cCountry = Range(FirstVizCell).Value
                'This updates and refreshes the PIVOT table
    '            With pt
    '                Field.ClearAllFilters
    '                Field.CurrentPage = cCountry
    '                pt.RefreshTable
    '            End With
            End With
        Else
            
        End If
        
    End Sub

  6. #6
    Forum Contributor
    Join Date
    03-23-2012
    Location
    Michigan
    MS-Off Ver
    MS365 Excel version 2301 (Build 16026.20238)
    Posts
    105

    Re: Update a pivot chart from a slicer change to a table

    Hi. Thanks for looking at this. I tried what you suggested, and it's still not triggering the event when I change the Slicer. I did sort of a work around that isn't ideal, but it works. In the email sub, I added the code below. Since that's in a regular sub and actually changes the value, it triggers the worksheet_change event, when changes the pivot table/chart. Thank you for taking the time to help!

    myZvalue = Range("Z1").Value
    If myZvalue = 1 Then
    Range("Z1").Value = 2
    Else
    If myZvalue = 2 Then
    Range("Z1").Value = 1
    End If
    End If

+ 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] Changing Slicer And Timeline Doesn't Change The Focus On The Table And Chart
    By zanshin777 in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 10-02-2020, 09:30 AM
  2. Disconnect Slicer, Change Pivot Source, Reonnect Slicer
    By ensi in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-27-2020, 04:26 AM
  3. Pivot Table & Chart not responding consistently with Data Slicer
    By mjacobse in forum Excel Charting & Pivots
    Replies: 3
    Last Post: 04-04-2019, 03:28 PM
  4. Refresh Pivot Table w/VBA upon Slicer Selection Change
    By lsnelley in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 03-28-2019, 01:05 PM
  5. [SOLVED] Pivot chart won't change when I add months using the slicer
    By Mrs.H in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 12-02-2015, 04:59 PM
  6. [SOLVED] Change Chart Title Depending on a Slicer - slicer advice
    By JungleJme in forum Excel General
    Replies: 8
    Last Post: 08-17-2012, 07:59 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