I've done a lot with VBA, but never anything relating to charts. I have a macro, courtesy of www.TheSpreadsheetGuru.com, which adjusts the Min/Max of the Y-Axis of a chart (see below). I would like to make it so that a zero baseline is always visible. If the data points go below 0, then the zero baseline would be somewhere in the middle of the graph. If the data points do not go below zero, then the bottom of the Y-Axis would begin at zero, regardless what the max is. Any suggestions?
As a bonus: any suggestions on how to always round the max up to the nearest 100?
Here's the code I have:
As a bonus: any suggestions on how to always round the max up to the nearest 100?
Here's the code I have:
Code:
Dim cht As ChartObject
Dim srs As Series
Dim FirstTime As Boolean
Dim MaxNumber As Double
Dim MinNumber As Double
Dim MaxChartNumber As Double
Dim MinChartNumber As Double
With Worksheets("KPMs 2018").ChartObjects("Chart 1")
.Activate
'Determine Chart's Overall Max/Min From Connected Data Source
For Each srs In ActiveChart.SeriesCollection
'Determine Maximum value in Series
MaxNumber = Application.WorksheetFunction.Max(srs.Values)
'Store value if currently the overall Maximum Value
If FirstTime = True Then
MaxChartNumber = MaxNumber
ElseIf MaxNumber > MaxChartNumber Then
MaxChartNumber = MaxNumber
End If
'Determine Minimum value in Series (exclude zeroes)
MinNumber = Application.WorksheetFunction.Min(srs.Values)
'Store value if currently the overall Minimum Value
If FirstTime = True Then
MinChartNumber = MinNumber
ElseIf MinNumber < MinChartNumber Or MinChartNumber = 0 Then
MinChartNumber = MinNumber
End If
End With
'Rescale Y-Axis
ActiveChart.Axes(xlValue).MinimumScale = Round(MinChartNumber / 100) * 100
ActiveChart.Axes(xlValue).MaximumScale = Round(MaxChartNumber / 100) * 100