Microsoft Office Tutorials and References
In Depth Information
Figure 21-9. Children of the Chart object
The Chart object has a read-write property named HasAxis that determines whether or not the
chart displays various types of axes. However, it is important to note that this method will fail if
the chart does not yet have the corresponding data series. For instance, if you are creating a chart
from scratch, it might be natural to add the code to create a category axis before adding the
category data that will be plotted against that axis. This not to do (to quote Hamlet)!
In fact, referring to the CreateChart example procedure earlier in the chapter, if we reverse the
data series code and the axes-related code, changing this:
' Add data series using data on the sheet itself
co.Chart.SeriesCollection.Add _
Source:=ActiveSheet.Range("A1:B6"), _
Rowcol:=xlColumns, SeriesLabels:=True, _
Categorylabels:=True
' Add axes
With co.Chart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlCategory, xlSecondary) = False
.HasAxis(xlValue, xlPrimary) = True
.HasAxis(xlValue, xlSecondary) = False
End With
to this:
' Add axes
With co.Chart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlCategory, xlSecondary) = False
.HasAxis(xlValue, xlPrimary) = True
.HasAxis(xlValue, xlSecondary) = False
End With
' Add data series using data on the sheet itself
co.Chart.SeriesCollection.Add _
Source:=ActiveSheet.Range("A1:B6"), _
Rowcol:=xlColumns, SeriesLabels:=True, _
Categorylabels:=True
Excel will issue the completely useless error message: "Method 'HasAxis' of object '_Chart' has
failed." (Unfortunately, as is all too often the case, the documentation does not discuss this issue at
all.)
The syntax for the HasAxis property is:
ChartObject .HasAxis( Index1 , Index2 )
where the parameters, despite their generic names, correspond directly to the Type and
AxisGroup parameters of the Axes method. For instance, the following code displays a primary
category axis and both primary and secondary value axes for the active chart:
With ActiveChart
.HasAxis(xlCategory, xlPrimary) = True
.HasAxis(xlCategory, xlSecondary) = False
.HasAxis(xlValue, xlPrimary) = True
.HasAxis(xlValue, xlSecondary) = True
End With
Search JabSto ::




Custom Search