Microsoft Office Development and Consultancy
 Home|

Excel

|VBA|C#|Finance|Tools|Newsletter|Feedback|Contact 
 Excel > Charts > VBA Code > Activating< Previous | Next > 

 

Activating

 
 

If you use the macro recorder to record chart related macros you will find that the code generated always activates the chart and then selects the objects that are being manipulated

 
 

This is not actually necessary. It is possible to modify a chart without actually selecting, or even activating the chart.

 

 

If you reference the ActiveChart and a chart has not been activated then an error will be generated.

 
 

Once a chart is activated you can refer to it in your code as ActiveChart.

 

 

To modify a chart using VBA it is not necessary to activate it.

 

 

When you activate a chart contained in a ChartObject, the chart actually is contained in a window that is normally invisible.

 
 

To see an embedded chart in its own window, right click the chart object and select Chart Window on the shortcut menu.

 
 

The embedded chart remains on the worksheet but the chart also appears in its own floating window.

 
 

You can move and resize this window, but you cannot maximise it.

 
 

If you move the window you will notice that the embedded chart is still displayed in its original location.

 
 

Activating any other window makes the ChartObject window invisible again.

 

 

Determining whether a Chart is Activated

 
 

The following line of code will only work for chart sheets and not for embedded charts

 
 
1
2
If TypeName(Selection) = "Chart"
If TypeName(Selection) = "ChartObject"
   

 

Theonly way to check if a chart is active that will work for both embedded charts and chart sheets is to force an error.

 
 
3
4
5
6
7
8
9
10
11
Function ChartIsSelected() As Boolean
Dim sChartName As String
   On Error GoTo AnError
   ChartIsSelected = True
   sChartName = ActiveChart.Name
   Exit Function
AnError:
   ChartIsSelected = False
End Function
   


 
12
ActiveChart.DeSelect
   





 © Better Solutions Limited 10-May-2013< Previous | Top | Next >