Using Object Variables to Streamline Code

Object variables make for much simpler and more efficient code. With an object variable, you can refer to a chart without actually activating the chart. In the confusing world of Charts and ChartObjects and ChartSheets, it is easier to define an object variable as a Chart object and then refer to that variable instead of ActiveChart or

Worksheets("Sheetl").ChartObject("Chart 2").Chart. To define an object variable as a Chart, use this line of code:

Dim Cht as Chart Set Cht = Charts.Add

Cht.SourceData = Source:=Sheets("Sheet4").Range("A1:B5")

As you go through the following section to learn how to customize various chart options, we'll use object variables extensively.

Another advantage of declaring object variables is that the Visual Basic Editor (VBE) can help you write code. In the VBE, go to Tools, Options and select the Editor tab. Make sure that "Auto List Members" is checked (see Figure 10.7).

0 0

Post a comment