Excel VBA - Automatically Adjust Pivot Chart Min/Max

camccull

New Member
Joined
Apr 8, 2016
Messages
11
I am trying to create a macro that automatically updates the y-axis min/max for two different pivot charts that are on the same sheet. I have 50 sheets in my workbook that I need to do this for.

Does anyone know VBA for this?

Thanks,
Claire

Using:
Excel 2016
Windows 7
 
Try the following macro, which assumes that the workbook contain your pivot charts is the active workbook, and that cells B1 and B2 in Sheet1 contain the minimum and maximum values. Note that it doesn't contain any error handling.

Code:
Option Explicit

Sub AdjustAllPivotCharts()

    Dim ws As Worksheet
    Dim oChrtObj As ChartObject
    Dim Min As Double
    Dim Max As Double
    
    Min = Worksheets("Sheet1").Range("B1").Value
    Max = Worksheets("Sheet1").Range("B2").Value
    
    For Each ws In ActiveWorkbook.Worksheets
        For Each oChrtObj In ws.ChartObjects
            With oChrtObj.Chart.Axes(Type:=xlValue, AxisGroup:=xlPrimary)
                .MinimumScale = Min
                .MaximumScale = Max
            End With
        Next oChrtObj
    Next ws
    
End Sub

In addition, to exclude a worksheet, let's say Sheet1, you can do the following...

Code:
    For Each ws In ActiveWorkbook.Worksheets
        [COLOR=#ff0000]If ws.Name <> "Sheet1" Then[/COLOR]
            For Each oChrtObj In ws.ChartObjects
                With oChrtObj.Chart.Axes(Type:=xlValue, AxisGroup:=xlPrimary)
                    .MinimumScale = Min
                    .MaximumScale = Max
                End With
            Next oChrtObj
       [COLOR=#ff0000] End If[/COLOR]
    Next ws

Hope this helps!
 
Upvote 0
Thank you so much! I will try this out and let you know how it works. If there were multiple sheets that I did not want the macro to run on, say 'Sheet2' and 'Sheet3', how would I add them to the code?
 
Upvote 0
Try...

Code:
    For Each ws In ActiveWorkbook.Worksheets
        [COLOR=#ff0000]If ws.Name <> "Sheet2" And ws.Name <> "Sheet3" Then[/COLOR]
            For Each oChrtObj In ws.ChartObjects
                With oChrtObj.Chart.Axes(Type:=xlValue, AxisGroup:=xlPrimary)
                    .MinimumScale = Min
                    .MaximumScale = Max
                End With
            Next oChrtObj
        End If
    Next ws
 
Upvote 0

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