Rescale Chart Y-Axis (VBA)

ExcelIntern34

New Member
Joined
Nov 17, 2016
Messages
1
So for my job I've been working on a financial dashboard and I have a HOLC chart that I have been using a macro to rescale price axes when new data is pulled. In addition to the HOLC chart I also have a pie chart (pivot chart) on the same page. The macro I have been using loops through chart series to scale the Y-Axis (useful when switching between a stock worth $30 and one worth $300). My issue is that I only want the macro to work on the HOLC chart (Chart 1) and not the pie chart. It's been all good until it tells me to debug the macro when it loops to the pie chart. The macro is from the SpreadSheetGuru's code vault. I'm new to loops and to using VBA on charts specifically so I haven't been able to find a fix.

Code:

Sub ReScaleChartAxis()




Dim cht As ChartObject
Dim srs As Series
Dim rng As Range
Dim ScaleMax As Boolean
Dim ScaleMin As Boolean
Dim chtMIN As Long
Dim chtMAX As Long
Dim Padding As Long


'Inputs (True = On / False = Off)
ScaleMax = False
ScaleMin = True
Padding = 10


'Optimize Code
Application.ScreenUpdating = False
Application.EnableEvents = False


'Loop through each chart in activesheet
For Each cht In ActiveSheet.ChartObjects

'Loop through each series in chart
For Each srs In cht.Chart.SeriesCollection
'Gather all data points used in chart
If rng Is Nothing Then
Set rng = Range(Split(srs.Formula, ",")(2))
Else
Set rng = Union(rng, Range(Split(srs.Formula, ",")(2)))
End If
Next srs

'Determine Max/Min of data points used in chart
chtMIN = Application.WorksheetFunction.Min(rng)
chtMAX = Application.WorksheetFunction.Max(rng)

With cht.Chart.Axes(xlValue)
'Adjust Y-Axis Max Value
If ScaleMax Then .MaximumScale = chtMAX + Padding
'Adjust Y-Axis Min Value
If ScaleMin Then .MinimumScale = chtMIN - Padding
End With


Next cht


'Reset Optimization
Application.EnableEvents = True
Application.ScreenUpdating = True


End Sub
 

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