18.11.2014 Views

Microsoft Office

Create successful ePaper yourself

Turn your PDF publications into a flip-book with our unique Google optimized e-Paper software.

Part VI<br />

Programming Excel with VBA<br />

The following macro performs the same function but works on all chart sheets in the active workbook:<br />

Sub ChartType2()<br />

Dim Cht As Chart<br />

For Each Cht In ActiveWorkbook.Charts<br />

Cht.ChartType = xlColumnClustered<br />

Next Cht<br />

End Sub<br />

Modifying chart properties<br />

The following example changes the legend font for all charts that are on the active sheet. It uses a For-<br />

Next loop to process all ChartObject objects and uses the HasLegend property to ensure that the chart<br />

has a legend. The code then adjusts the properties of the Font object contained in the Legend object.<br />

Sub LegendMod()<br />

Dim ChtObj As ChartObject<br />

For Each ChtObj In ActiveSheet.ChartObjects<br />

ChtObj.Chart.HasLegend = True<br />

With ChtObj.Chart.Legend.Font<br />

.Name = “Arial”<br />

.FontStyle = “Bold”<br />

.Size = 8<br />

End With<br />

Next ChtObj<br />

End Sub<br />

Applying chart formatting<br />

This example applies several different formatting types to the specified chart (in this case, Chart 1 on the<br />

active sheet).<br />

Sub ChartMods()<br />

With ActiveSheet.ChartObjects(“Chart 1”).Chart<br />

.ChartType = xlArea<br />

.ChartArea.Font.Name = “Arial”<br />

.ChartArea.Font.FontStyle = “Regular”<br />

.ChartArea.Font.Size = 9<br />

.PlotArea.Interior.ColorIndex = 6<br />

.Axes(xlValue).TickLabels.Font.Bold = True<br />

.Axes(xlCategory).TickLabels.Font.Bold = True<br />

End With<br />

End Sub<br />

One way to learn about these properties is to record a macro while you apply various changes to a chart.<br />

VBA Speed Tips<br />

VBA is fast, but it’s often not fast enough. This section presents programming examples that you can use to<br />

help speed your macros.<br />

766

Hooray! Your file is uploaded and ready to be published.

Saved successfully!

Ooh no, something went wrong!