Microsoft Office Tutorials and References
In Depth Information
Determining whether the active chart is embedded
Sub AddTitle()
If ActiveChart Is Nothing Then
MsgBox “Select a chart.”
Exit Sub
End If
With ActiveChart
.HasTitle = True
.ChartTitle.Text = Range(“A1”)
End With
End Sub
Determining whether the active chart is embedded
As you know, a chart can be either of two types: an embedded chart or a chart on
a Chart sheet. In some cases, your macro may need to make this determination. The
TypeOfSelection macro, which follows, displays one of three messages.
Sub TypeOfSelection()
If ActiveChart Is Nothing Then
MsgBox “No Chart”
Exit Sub
End If
If TypeName(ActiveChart.Parent) = “Workbook” Then
MsgBox “Chart Sheet”
Exit Sub
End If
If TypeName(ActiveChart.Parent) = “ChartObject” Then
MsgBox “Embedded Chart”
End If
End Sub
The TypeOfSelection macro first checks for whether a chart is active. If not, it
displays No Chart and the macro ends. The next two If statements use VBA’s
TypeName function to determine the object type of the active chart’s “parent” object
(an object’s parent is the object that contains it). The Chart object in an embedded
chart is contained in a ChartObject object. The ChartObject ’s parent is the
worksheet on which it is embedded. The parent of a Chart sheet is the Workbook object
that contains it.
Identifying the Selected Object
The WhatIsSelected macro, which follows, uses VBA’s TypeName function to
determine the type of object that is currently selected. For example, it will display
Search JabSto ::




Custom Search