Update Chart VBA

Claymationator

Well-known Member
Joined
Sep 26, 2006
Messages
705
I have a dashboard that I have created where the user can chart out different metrics over a period of time that they select. The metrics themselves could be in the millions, or a %. So, the code that I am trying to use will update the chart based on the selections and update the axis, as well as place certain metrics on the secondary axis if necessary.

Here is the code that I am using:

Code:
Sub ReformatChart()
    'Application.Calculation = xlManual
    Application.ScreenUpdating = False
    Calculate
    ActiveSheet.ChartObjects("Chart 3").Activate
    
        ActiveChart.SeriesCollection(1).AxisGroup = Worksheets("ChartData").Range("P11")
        ActiveChart.SeriesCollection(2).AxisGroup = Worksheets("ChartData").Range("T11")
        ActiveChart.SeriesCollection(3).AxisGroup = Worksheets("ChartData").Range("X11")
        ActiveChart.SeriesCollection(4).AxisGroup = Worksheets("ChartData").Range("AB11")

        ActiveChart.Axes(xlValue).MaximumScale = Worksheets("ChartData").Range("P19")
        ActiveChart.Axes(xlValue).MinimumScale = Worksheets("ChartData").Range("P20")
        ActiveChart.Axes(xlValue, xlSecondary).MaximumScale = Worksheets("ChartData").Range("P21")
        ActiveChart.Axes(xlValue, xlSecondary).MinimumScale = Worksheets("ChartData").Range("P22")
        
        If Worksheets("ChartData").Range("P36") = True Then
            ActiveChart.Axes(xlValue).Select
            Selection.TickLabels.NumberFormat = "0.0%;(0.0%)"
        Else
            ActiveChart.Axes(xlValue).Select
            Selection.TickLabels.NumberFormat = "_(* #,##0_);_(* (#,##0);_(* " - "??_);_(@_)"
        End If
        
        If Worksheets("ChartData").Range("K23") = 0 Then
            ActiveChart.Axes(xlValue, xlSecondary).Select
            Selection.TickLabels.Font.ColorIndex = 2
        Else
            ActiveChart.Axes(xlValue, xlSecondary).Select
            Selection.TickLabels.Font.ColorIndex = 1
        End If
        
    Calculate
    Range("A1").Select
    Application.ScreenUpdating = True

End Sub

However, I am getting an error stating "Method 'Axes' of object '_Chart' failed" with this line highlighted:

Code:
        ActiveChart.Axes(xlValue, xlSecondary).MaximumScale = Worksheets("ChartData").Range("P21")

However, I am not understanding why it isn't working. I have used this method in the past, and can't figure out why it is breaking at this point.

Any help is appreciated.
 

Excel Facts

When did Power Query debut in Excel?
Although it was an add-in in Excel 2010 & Excel 2013, Power Query became a part of Excel in 2016, in Data, Get & Transform Data.

Forum statistics

Threads
1,216,763
Messages
6,132,584
Members
449,737
Latest member
naes

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