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
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