Super 2-axis chart Autoscale Macro ;-)
Thx Jon, it works fine.
I tried to make it easier for my CPU avoiding loops (I have hundreds of
charts to scale :)
I experience an error code 2042: t_prim_max = WorksheetFunction.max(sr.Values)
( see line ' ERROR bcs error 2042 )
I now need to use the Excel autoscale capability ONLY IF the above function
returns an error 2042 (because of #N/A in series, data is missing). to get BY
DEFAULT a more or less properly scaled chart.
My problem is that WorksheetFunction.max(sr.Values) returns Empty so my code
uses the last value given to t_prim_max :(
Ho do I avoid that without looping ? :-)
Thx a lot !
below is my code (novice code!) :
----------------------------
Sub scale_all()
step = 5 'entrer ici la Major Unit
Application.ScreenUpdating = False
For Each cht_obj In ActiveSheet.ChartObjects
cht_obj.Activate
On Error Resume Next
prim_max = -1000
prim_min = 1000
sec_max = -1000
sec_min = 1000
Set cht = ActiveChart
For Each sr In cht.SeriesCollection
If sr.AxisGroup = xlPrimary Then
t_prim_max = WorksheetFunction.max(sr.Values) ' ERROR bcs error 2042
t_prim_min = WorksheetFunction.min(sr.Values)
If t_prim_max prim_max Then
prim_max = t_prim_max
End If
If t_prim_min < prim_min Then
prim_min = t_prim_min
End If
With ActiveChart.Axes(xlValue)
.MinimumScale = prim_min
.MaximumScale = prim_max
.MinorUnitIsAuto = True
.MajorUnit = (prim_max - prim_min) / step
.Crosses = xlAutomatic
.ReversePlotOrder = False
.ScaleType = xlLinear
.DisplayUnit = xlNone
End With
End If
Next
For Each sr In cht.SeriesCollection
If sr.AxisGroup = xlSecondary Then
t_sec_max = WorksheetFunction.max(sr.Values)
t_sec_min = WorksheetFunction.min(sr.Values)
End If
If t_sec_max sec_max Then
sec_max = t_sec_max
End If
If t_sec_min < sec_min Then
sec_min = t_sec_min
End If
With ActiveChart.Axes(xlValue, xlSecondary)
.MinimumScale = sec_min
.MaximumScale = sec_max
.MinorUnitIsAuto = True
.MajorUnit = (sec_max - sec_min) / step
.Crosses = xlAutomatic
.ReversePlotOrder = False
.ScaleType = xlLinear
.DisplayUnit = xlNone
End With
Next
Next
ActiveChart.Deselect
Application.ScreenUpdating = True
End Sub
-------------------
|