I am creating an script that auto-formats charts because re-doing all of these steps over and over again is a bit of a time suck. I currently have a script that changes all series colors, line thickness, re-sizes the area, and some other smaller things.
Public Sub ChartAlt()
'
' ChartAlt Macro
'
' Keyboard Shortcut: Ctrl+Shift+A
'
'keeps on chugging if it finds an error (turn off/comment out before editing and testing code)
On Error Resume Next
If MsgBox("Have you saved before running this prompt? Saving will allow you to exit and re-open the file to before the changes were made. Macros cannot be undone.", vbYesNo) = vbNo Then Exit Sub
With ActiveChart
.HasTitle = True 'turns on title
.SetElement (msoElementChartTitleAboveChart) 'places title above chart
.SetElement (msoElementLegendBottom) 'moves legend to bottom
.HasDataTable = False 'turns off data table
.ChartArea.Format.Line.Visible = msoFalse 'removes border
.ShowAllFieldButtons = False ' turns off field buttons (pivot charts only)
End With
' Turns on legend if more than one series exists
If ActiveChart.SeriesCollection.Count >= 2 Then
ActiveChart.HasLegend = True
Else
ActiveChart.HasLegend = False
End If
' resizes the chart to 7" wide and 4" tall
With ActiveChart.Parent
.Height = 288
.Width = 504
.Placement = xlFreeFloating
End With
' Changes all Series color purple using incrementing transparencies
Dim mySeries As Series
Dim seriesCol As FullSeriesCollection
Dim i As Integer, J As Variant, UWColor As Long
i = 1
J = 1 / (ActiveChart.SeriesCollection.Count + 1) 'creates a percentage transparency based on # of series
UWColor = RGB(51, 0, 111) 'color taken from UW website
Set seriesCol = ActiveChart.FullSeriesCollection
For Each mySeries In seriesCol
Set mySeries = ActiveChart.FullSeriesCollection(i)
With mySeries
.Format.Line.ForeColor.RGB = UWColor
.Format.Line.Transparency = 0.8 - (i * J) 'a lower 0.X means darker lines
.Format.Fill.ForeColor.RGB = UWColor
.Format.Fill.Transparency = 1.2 - (i * J) 'a higher 1.X means lighter fills
'checks for series type and adjusts line/bar size
If .ChartType = xlBarStacked Then
.Format.Line.Weight = 0.5
ActiveChart.ChartGroups(i).GapWidth = 50
ElseIf .ChartType = xlBarClustered Then
.Format.Line.Weight = 0.5
ActiveChart.ChartGroups(i).GapWidth = 50
ElseIf .ChartType = xlColumnClustered Then
.Format.Line.Weight = 0.5
ActiveChart.ChartGroups(i).GapWidth = 50
ElseIf .ChartType = xlBarStacked100 Then
.Format.Line.Weight = 0.5
ActiveChart.ChartGroups(i).GapWidth = 50
ElseIf .ChartType = xlLine Then
.Format.Line.Weight = 2
ElseIf .ChartType = xlLineMarkers Then
.Format.Line.Weight = 2
'Line markers have an issue with colors, this is a temporary solution
.MarkerBackgroundColorIndex = xlColorIndexAutomatic
.MarkerForegroundColorIndex = xlColorIndexNone
Else
.Format.Line.Weight = 1
End If
End With
i = i + 1
Next
' turns axis on, changed colors black, and adds a line
With ActiveChart
For Each a In .Axes
a.TickLabels.Font.Color = "black"
a.TickLabels.Font.Size = 10
a.TickLabels.Font.Bold = False
a.Format.Line.Visible = msoTrue
a.Format.Line.ForeColor.ObjectThemeColor = msoThemeColorText1
a.Format.Line.ForeColor.TintAndShade = 0
a.Format.Line.ForeColor.Brightness = 0
a.HasMajorGridlines = False
a.HadMinorGridlines = False
a.HasTitle = True
a.AxisTitle.Format.TextFrame2.TextRange.Font.Fill.Visible = msoTrue
a.AxisTitle.Format.TextFrame2.TextRange.Font.Fill.ForeColor.RGB = RGB(0, 0, 0)
a.AxisTitle.Format.TextFrame2.TextRange.Font.Fill.Transparency = 0
a.AxisTitle.Format.TextFrame2.TextRange.Font.Fill.Solid
Next a
End With
ActiveChart.Legend.Select
With Selection.Format.TextFrame2.TextRange.Font.Fill
.Visible = msoTrue
.ForeColor.RGB = RGB(0, 0, 0)
.Transparency = 0
.Solid
End With
ActiveChart.ChartTitle.Select
With Selection.Format.TextFrame2.TextRange.Font
.Fill.Visible = msoTrue
.Fill.ForeColor.RGB = RGB(0, 0, 0)
.Fill.Transparency = 0
.Fill.Solid
.Size = 16
End With
End Sub
One is issue is that if there is a line series number lower than a bar series number, the line is hidden behind the bar (on a combo chart).
Is there a way to have the script identify if the series is a line type and then move that series to the top of the chart so it is not hidden behind any bars? Basically trying to say "if series is a line, then change series number to [series count +1]" (i think).
Your help is appreciated.
Series
has a.ChartType
property, you could check whether that series is.ChartType = xlLine
(or modify to the correct constant/enumeration), and compare against the corresponding data point in another series. EachDataLabel
has a.Top
,.Left
,.Width
and.Height
property, so you could simply make some adjustment to those to move the label elsewhere within the chart. – David Zemens