Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Excel macro to make clustered charts' axes in the book start from zero.
Sub FixChartAxes()
'
' FixChartAxes Macro
'
' Define chart types to process
Dim chart_types As New Collection
With chart_types
.Add Item:=xlBarClustered
.Add Item:=xlColumnClustered
.Add Item:=xl3DBarClustered
.Add Item:=xl3DColumnClustered
.Add Item:=xlConeBarClustered
.Add Item:=xlConeColClustered
.Add Item:=xlCylinderBarClustered
.Add Item:=xlCylinderColClustered
.Add Item:=xlPyramidBarClustered
.Add Item:=xlPyramidColClustered
End With
On Error GoTo noChartSheet
' MsgBox ("Number of chart sheets: " & ActiveWorkbook.Charts.Count)
For i = 1 To ActiveWorkbook.Charts.Count
With ActiveWorkbook.Charts(i)
For c = 1 To chart_types.Count
If .charttype = chart_types(c) Then
.Axes(xlValue).MinimumScale = 0
End If
Next
End With
Next
On Error GoTo noEmbedChart
For i = 1 To ActiveWorkbook.Worksheets.Count
With ActiveWorkbook.Worksheets(i)
' MsgBox ("Worksheet name: " & .Name & ", Number of embedded charts: " & .ChartObjects.Count)
For j = 1 To .ChartObjects.Count
With .ChartObjects(j).Chart
For c = 1 To chart_types.Count
If .charttype = chart_types(c) Then
.Axes(xlValue).MinimumScale = 0
End If
Next
End With
Next
End With
Next
Exit Sub
noChartSheet:
' MsgBox "There is no chart sheet in this book."
noEmbedChart:
' MsgBox "There is no embedded chart in this worksheet."
End Sub
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment