Slicer click button handler to update a chart

hcova

New Member
Joined
Jul 29, 2010
Messages
30
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
Hi everyone.
I have created a PivotTable slicer to display different charts according the user's button selection.
I am using a combo chart and I need that 0 value in the primary axis and in the secondary axis to be at the same horizontal level.
Let explain it with the following chart. (The slicer by default gives me the below chart).

1732838714976.png


As you can see in the above combo chart, the 0 value of both Y axis are not at the same horizontal label

Then, this is what I need.......

1732838837861.png


Now, as you can see, the 0 value of the Y-axis coincides horizontally on both axes because I had to manually adjust the maximum and minimum values of the secondary axis

Question.

How can I write a VBA code (I know that Excel does not provide a slicer handler, just pivot tables handlers) to modify the secondary axis bounds each time a slicer button is clicked?

Second, where should I write the code? In the pivot table sheet? in the chart sheet? or in a vba module?

1732840104250.png


User NameClaude
Row LabelsProfitAccumulated Profit
201820,4381,055,952
Qtr115,565227,627
Qtr22,900381,013
Qtr3-344276,657
Qtr42,316170,655
201931,5852,116,168
Qtr14,504425,574
Qtr219,494436,959
Qtr3-10,902589,310
Qtr418,488664,325
202098,5736,022,953
Qtr13,771866,329
Qtr213,1501,189,873
Qtr338,2311,050,912
Qtr443,4212,915,839
202126,82511,022,305
Qtr11,4772,469,449
Qtr215,9792,825,337
Qtr3-5,8363,130,123
Qtr415,2052,597,396
2022-20,3438,928,503
Qtr1-3,9412,404,155
Qtr2-21,5371,991,084
Qtr39,2592,368,004
Qtr4-4,1242,165,261
202359,81913,617,714
Qtr142,6932,794,618
Qtr23,7424,492,471
Qtr3-5,8502,797,980
Qtr419,2343,532,646
202465,53511,158,151
Qtr115,7252,755,979
Qtr222,4142,851,947
Qtr328,7283,019,839
Qtr4-1,3322,530,385
Grand Total282,43153,921,746
 

Excel Facts

Move date out one month or year
Use =EDATE(A2,1) for one month later. Use EDATE(A2,12) for one year later.
How can I write a VBA code (I know that Excel does not provide a slicer handler, just pivot tables handlers) to modify the secondary axis bounds each time a slicer button is clicked?

Second, where should I write the code? In the pivot table sheet? in the chart sheet? or in a vba module?

The starting point would be the Worksheet_PivotTableUpdate handler in the sheet module of the pivot table sheet. For example, this simply displays the selected slicer item(s):

VBA Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    
    Dim theSlicer As Slicer
    Dim selectedSlicerItem As SlicerItem
    
    Set theSlicer = Target.Slicers("SysName")
    
    For Each selectedSlicerItem In theSlicer.SlicerCache.VisibleSlicerItems
        MsgBox "Item: " & selectedSlicerItem.Name
    Next
    
End Sub

We would probably need a sample workbook to help you further with this.
 
Upvote 0

Forum statistics

Threads
1,224,825
Messages
6,181,189
Members
453,020
Latest member
Mohamed Magdi Tawfiq Emam

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top