loop through chart on sheet to change axis numbers

frontdeskcps

New Member
Joined
Dec 7, 2018
Messages
4
I am very new to VBA and am having a hard time understanding how the loop feature is working in relation to only charts on the current sheet. I can change an "active chart" with a VBA but need to loop it through all charts on the current sheet. I have been trying to understand it for 2 days of intense research but am lost. I only want to change the Yaxis(xlValue). The VBA I am using to make a change the value is:

Sub ScaleAxes100()

With Application.ActiveChart.Axes(xlValue, xlPrimary)
.MinimumScale = ActiveSheet.Range("af3").Value
.MaximumScale = ActiveSheet.Range("af2").Value
.MajorUnit = ActiveSheet.Range("af4").Value

End With

End Sub

I can do it with one chart but there are 24 charts on each sheet and 42 sheets to "fix".
 

Excel Facts

Remove leading & trailing spaces
Save as CSV to remove all leading and trailing spaces. It is faster than using TRIM().
Untested...
Code:
Dim Chrt As Chart
For Each Chrt In ActiveSheet.ChartObjects
With .Chart.Axes(xlValue)
.MinimumScale = ActiveSheet.Range("af3").Value
 .MaximumScale = ActiveSheet.Range("af2").Value
 .MajorUnit = ActiveSheet.Range("af4").Value
End With
With .Chart.Axes(xlCategory)
.MinimumScale = ActiveSheet.Range("af3").Value
 .MaximumScale = ActiveSheet.Range("af2").Value
 .MajorUnit = ActiveSheet.Range("af4").Value
End With
Next Chrt
HTH. Dave
ps. Welcome to the Board! Please use code tags.
 
Last edited:
Upvote 0
First of all - Thanks
Second, what is a code tag?
Third, I am getting an error code: invalid or unqualified reference. The .Chart in line 3 is highlighted. Does it need to say "ActiveChart"?
 
Upvote 0
Thank you! Here's what finally worked.
"af2" is a cell reference where I put my max number ....

Code:
Sub SizeGraphs_PERCENT()


Dim ws As Worksheet
Dim objCht As ChartObject


       
        For Each objCht In ActiveSheet.ChartObjects
            With objCht.Chart
            With .Axes(xlValue)
                .MinimumScale = ActiveSheet.Range("af3").Value
                .MaximumScale = ActiveSheet.Range("af2").Value
                .MajorUnit = ActiveSheet.Range("af4").Value
            End With
            End With
        Next objCht


End Sub

Still not sure if I have this code tag set up correctly. If I don't, sorry!
 
Upvote 0

Forum statistics

Threads
1,214,982
Messages
6,122,575
Members
449,089
Latest member
Motoracer88

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