Hi
I have been trying to zero down to a vba code for a task.
I have 2 charts in excel and need to autoadjust the scale of Y axes to maximum among both of them so that both scale are equal.
I have following code but it only works for 1st time and the next time scale is not reset again before the next selection.
As an example, 1st screenshot is before vba code runs. and 2nd image is where the scale sets to 45 as max value for both which is what I want. But when the values in source data change next time the max value remains at 45 and is not reset.
Any suggestions to edit the code?
Thanks
I have been trying to zero down to a vba code for a task.
I have 2 charts in excel and need to autoadjust the scale of Y axes to maximum among both of them so that both scale are equal.
I have following code but it only works for 1st time and the next time scale is not reset again before the next selection.
VBA Code:
Sub AdjustYAxis2()
Dim cht1 As Chart
Dim cht2 As Chart
Dim maxVal As Double
Set cht1 = ActiveSheet.ChartObjects("Chart 2").Chart
Set cht2 = ActiveSheet.ChartObjects("Chart 5").Chart
If cht1.HasAxis(xlValue) And cht2.HasAxis(xlValue) Then
maxVal = Application.WorksheetFunction.Max(cht1.Axes(xlValue).MaximumScale, cht2.Axes(xlValue).MaximumScale)
cht1.Axes(xlValue).MinimumScale = 0
cht1.Axes(xlValue).MaximumScale = maxVal
cht2.Axes(xlValue).MinimumScale = 0
cht2.Axes(xlValue).MaximumScale = maxVal
Else
MsgBox "One or both of the charts do not have a Y-axis."
End If
End Sub
As an example, 1st screenshot is before vba code runs. and 2nd image is where the scale sets to 45 as max value for both which is what I want. But when the values in source data change next time the max value remains at 45 and is not reset.
Any suggestions to edit the code?
Thanks