VBA code to loop through all charts in each worksheet of a workbook

athaung

New Member
Joined
Jan 10, 2014
Messages
23
Hi,

I am trying to write code that allows me to go through each worksheet in my workbook (except a select few as shown in code below) and fix the y-scale of 4 different chart/graphs in EACH worksheet based on values in a worksheet. Below is the code I have so far. This changes the y-scale of all charts on the worksheet I am currently on, but does not cycle through all work sheets. Note that "Axis_min" and "Axis_max" are names I assigned using Excel: Formulas >Defined Names > Define Name. I used an =Indirect("A1") kind of formula so that the Axis_min and Axis_max will be different for each worksheet. Please help fix my code! I'll be forever grateful :biggrin:

Code:
Sub SizeGraphs()

Dim ws As Worksheet
Dim objCht As ChartObject

For Each ws In ThisWorkbook.Worksheets
        If ws.Name = "Master" Or ws.Name = "Template" Or ws.Name = "Start" Or ws.Name = "NQF 2015" Then
        
        Else
        
        For Each objCht In ActiveSheet.ChartObjects
            With objCht.Chart
            With .Axes(xlValue)
                .MaximumScale = ActiveSheet.Range("Axis_max").Value
                .MinimumScale = ActiveSheet.Range("Axis_min").Value
            End With
            End With
        Next objCht

    End If
Next ws
End Sub
 

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.
Try replacing...

Code:
For Each objCht In ActiveSheet.ChartObjects

with

Code:
For Each objCht In [COLOR=#ff0000]ws[/COLOR].ChartObjects

Hope this helps!
 
Upvote 0
Thanks!

I ended up with the following code that works:
Code:
For Each ws In Worksheets
    If ws.Name = "Master" Or ws.Name = "Template" Or ws.Name = "Start" Or ws.Name = "NQF 2015" Then
        
        Else
        
        ws.Activate
        For Each objCht In ws.ChartObjects
            With objCht.Chart
                With .Axes(xlValue)
                    .MaximumScale = ActiveSheet.Range("Axis_max").Value
                    .MinimumScale = ActiveSheet.Range("Axis_min").Value
                End With
            End With
        Next objCht

    End If
Next ws
 
Upvote 0

Forum statistics

Threads
1,222,545
Messages
6,166,710
Members
452,065
Latest member
Mondo8a

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