Update all charts instead of a specific one via macro

Joined
Jul 14, 2022
Messages
21
Office Version
  1. 2016
Platform
  1. Windows
I have a macro that's designed to change the minimum and maximum axis values for a chart based on values entered in a cell. Example below

VBA Code:
    ActiveSheet.ChartObjects("Chart 32").Chart.Axes(xlCategory).MinimumScale = Range("C2").Value
    ActiveSheet.ChartObjects("Chart 32").Chart.Axes(xlCategory).MaximumScale = Range("C3").Value

Rather than calling upon "Chart 32", I'd like to just update all charts on the active sheet (there are a lot). How would I go about this?

Thank you
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
Try the following...

VBA Code:
    Dim chrtObj As ChartObject
    
    For Each chrtObj In ActiveSheet.ChartObjects
        With chrtObj.Chart.Axes(xlCategory)
            .MinimumScale = Range("C2").Value
            .MaximumScale = Range("C3").Value
        End With
    Next chrtObj

Hope this helps!
 
Upvote 0
Solution
Try the following...

VBA Code:
    Dim chrtObj As ChartObject
   
    For Each chrtObj In ActiveSheet.ChartObjects
        With chrtObj.Chart.Axes(xlCategory)
            .MinimumScale = Range("C2").Value
            .MaximumScale = Range("C3").Value
        End With
    Next chrtObj

Hope this helps!
Worked perfectly, thank you! Any particular reason why you include the Dim chtO as ChartObject line? Could you just do For Each ChartObject in ActiveSheet.ChartObjects...?
 
Upvote 0
Worked perfectly, thank you!
You're very welcome!
Any particular reason why you include the Dim chtO as ChartObject line?
It's good practice to declare all your variables. I always place at the very top of any module the statement Option Explicit, which forces all variables to be declared. And it also helps to catch any spelling mistakes.
Could you just do For Each ChartObject in ActiveSheet.ChartObjects...?
Since ChartObject is actually an object type, it's best to avoid it.
 
Upvote 0
You're very welcome!

It's good practice to declare all your variables. I always place at the very top of any module the statement Option Explicit, which forces all variables to be declared. And it also helps to catch any spelling mistakes.

Since ChartObject is actually an object type, it's best to avoid it.
Interesting. I'm learning VBA as I come across reasons to use it, so I appreciate the explanation.
 
Upvote 0

Forum statistics

Threads
1,223,262
Messages
6,171,080
Members
452,377
Latest member
bradfordsam

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