Applying chart formatting

Vertex42 The Excel Nexus

Professional Excel Templates

Get Instant Access

This example applies several different types of formatting to the active chart:

Sub ChartMods()

ActiveChart.Type = xlArea ActiveChart.ChartArea.Font.Name = "Arial" ActiveChart.ChartArea.Font.FontStyle = "Regular" ActiveChart.ChartArea.Font.Size = 9 ActiveChart.PlotArea.Interior.Colorlndex = xlNone ActiveChart.Axes(xlValue).TickLabels.Font.Bold = True ActiveChart.Axes(xlCategory).TickLabels.Font.Bold = _ True

ActiveChart.Legend.Position = xlBottom End Sub

You must activate a chart before executing this macro. Activate an embedded chart by clicking it. To activate a chart on a chart sheet, activate the chart sheet.

To ensure that a chart is selected, you can add some error-handling code. (See Chapter 12 for details about error handling.) Here's the modified macro, which displays a message if a chart is not selected:

Sub

ChartMods2()

On Error GoTo ErrorHandler

ActiveChart.Type = xlArea

ActiveChart.ChartArea.Font.Name = "Arial"

ActiveChart.ChartArea.Font.FontStyle = "Regular"

ActiveChart.ChartArea.Font.Size = 9

ActiveChart.PlotArea.Interior.Colorlndex = xlNone

ActiveChart.Axes(xlValue).TickLabels.Font.Bold = True

ActiveChart.Axes(xlCategory).TickLabels.Font.Bold = _

True

ActiveChart.Legend.Position = xlBottom

Exit Sub

ErrorHandler:

MsgBox "Select a chart first."

End

Sub

I created this macro by recording my actions as I formatted a chart. Then I cleaned up the recorded code by removing irrelevant lines.

I created this macro by recording my actions as I formatted a chart. Then I cleaned up the recorded code by removing irrelevant lines.

Was this article helpful?

0 0

Post a comment