Adjusting scale on Slicer

MPaulus

New Member
Joined
Oct 13, 2021
Messages
11
Office Version
  1. 365
Platform
  1. Windows
I have created my pivot table and pivot chart. I have added the following code to have the scale on the secondary axis match the primary axis:

VBA Code:
Dim x As Long


    With ActiveSheet.ChartObjects("Pivot").Chart

        ActiveSheet.ChartObjects("Pivot").Activate
        ActiveChart.Axes(xlValue).MaximumScaleIsAuto = True
        ActiveChart.Axes(xlValue).MinimumScale = 0
        x = ActiveChart.Axes(xlValue).MaximumScale
        ActiveChart.Axes(xlValue, xlSecondary).MaximumScale = x

The problem is that now when I use my slicer the secondary axis scale remains the same as the summary and does not adjust to match the individual selections.
Summary.png
Work Center Selected.png
 

Excel Facts

Get help while writing formula
Click the italics "fx" icon to the left of the formula bar to open the Functions Arguments dialog. Help is displayed for each argument.
Here, I think you can use the PivotTableUpdate event handler. So, every time you select a slicer item, the pivot table will be updated, the event handler will be executed, and the secondary axis scale will be set according to the primary axis scale. Right-click the sheet tab, select View Code, and copy/paste the following code into the code module for the sheet. . .

VBA Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    With Target.PivotChart.Chart
        .Axes(xlValue, xlSecondary).MaximumScale = .Axes(xlValue).MaximumScale
    End With
End Sub

Hope this helps!
 
Upvote 0
I get a run-time error '1004': Application-defined or object-defined error. It points to
VBA Code:
With Target.PivotChart.Chart
 
Upvote 0
Interesting. Prior to posting my code, I tested it and it seemed to work fine. Now that I have tested it again, I get the same error as you. So assuming that the pivot chart is located on the same worksheet as the pivot table, right-click the sheet tab, select View Code, and copy/paste the following code into the code module for the sheet...

VBA Code:
Private Sub Worksheet_PivotTableUpdate(ByVal Target As PivotTable)
    With Me.ChartObjects(1).Chart
        .Axes(xlValue, xlSecondary).MaximumScale = .Axes(xlValue).MaximumScale
    End With
End Sub

Does this help?
 
Upvote 0
Solution

Forum statistics

Threads
1,214,932
Messages
6,122,332
Members
449,077
Latest member
jmsotelo

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