Class Axis (Excel VBA)
The class Axis represents a single axis in a chart.
The main procedures of class Axis are Delete and Select
Set
To use a Axis class variable it first needs to be instantiated, for example
Dim chr As Chart: Set chr =
Dim axiAxe As Axis
Set axiAxe = chr.Axes(Type:=xlCategory)
The following procedures can be used to set variables of type Axis: Chart.Axes and Axes.Item
For Each
Here is an example of processing the Axis items in a collection.
Dim chr As Chart: Set chr =
Dim axiAxe As Axis
For Each axiAxe In chr.Axes()
Next axiAxe
Themes
Some procedures in this class have been grouped together in themes and are described on separate theme pages
Name with its procedures AxisTitle, CategoryNames and HasTitle
Display/Show with its procedures DisplayUnit, DisplayUnitCustom and DisplayUnitLabel
Methods
These are the main methods of the Axis class
Delete - Deletes the object.
Select - Selects the object.
Other Methods
Axes.Item - Returns a single Axis object from an Axes collection.
With Charts("chart1").Axes.Item(xlCategory)
.HasTitle = True
.AxisTitle.Caption = "1994"
End With
Properties
Axes.Count returns a Long value that represents the number of objects in the collection.
Axes.Parent returns the parent object for the specified object. Read-only.
Sub DisplayParentName()
Set myAxis = Charts(1).Axes(xlValue)
MsgBox myAxis.Parent.Name
End Sub
AxisBetweenCategories true if the value axis crosses the category axis between categories.
Charts("Chart1").Axes(xlCategory).AxisBetweenCategories = True
AxisGroup returns the group for the specified axis.
BaseUnit returns or sets the base unit for the specified category axis.
With Worksheets(1).ChartObjects(1).Chart
With .Axes(xlCategory)
.CategoryType = xlTimeScale
.BaseUnit = xlMonths
End With
End With
BaseUnitIsAuto true if Microsoft Excel chooses appropriate base units for the specified category axis. The default value is True.
With Worksheets(1).ChartObjects(1).Chart
With .Axes(xlCategory)
.CategoryType = xlTimeScale
.BaseUnitIsAuto = True
End With
End With
Border returns a Border object that represents the border of the object.
CategorySortOrder
CategoryType returns or sets the category axis type.
With Worksheets(1).ChartObjects(1).Chart
With .Axes(xlCategory)
.CategoryType = xlTimeScale
.BaseUnit = xlMonths
End With
End With
Crosses returns or sets the point on the specified axis where the other axis crosses.
Charts("Chart1").Axes(xlCategory).Crosses = xlMaximum
CrossesAt returns or sets the point on the value axis where the category axis crosses it. Applies only to the value axis.
Sub Chart()
' Create a sample source of data.
Range("A1") = "2"
Range("A2") = "4"
Range("A3") = "6"
Range("A4") = "3"
' Create a chart based on the sample source of data.
Charts.Add
With ActiveChart
.ChartType = xlLineMarkersStacked
.SetSourceData Source:=Sheets("Sheet1").Range("A1:A4"), PlotBy:= xlColumns
.Location Where:=xlLocationAsObject, Name:="Sheet1"
End With
' Set the category axis to cross the value axis at value 3.
ActiveChart.Axes(xlValue).Select
Selection.CrossesAt = 3
End Sub
Format returns the ChartFormat object.
HasDisplayUnitLabel true if the label specified by the DisplayUnit or DisplayUnitCustom property is displayed on the specified axis. The default value is True.
With Charts("Chart1").Axes(xlValue)
.DisplayUnit = xlCustom
.DisplayUnitCustom = 500
.AxisTitle.Caption = "Rebate Amounts"
.HasDisplayUnitLabel = False
End With
HasMajorGridlines true if the axis has major gridlines. Only axes in the primary axis group can have gridlines.
With Charts("Chart1").Axes(xlValue)
If .HasMajorGridlines Then
.MajorGridlines.Border.ColorIndex = 3 'set color to red
End If
End With
HasMinorGridlines true if the axis has minor gridlines. Only axes in the primary axis group can have gridlines.
With Charts("Chart1").Axes(xlValue)
If .HasMinorGridlines Then
.MinorGridlines.Border.ColorIndex = 4
'set color to green
End If
End With
Height returns a Double value that represents the height, in points, of the object.
Left returns a Double value that represents the distance, in points, from the left edge of the object to the left edge of the chart area.
LogBase returns or sets the base of the logarithm when you are using log scales.
MajorGridlines returns a Gridlines object that represents the major gridlines for the specified axis. Only axes in the primary axis group can have gridlines.
With Charts("Chart1").Axes(xlValue)
If .HasMajorGridlines Then
.MajorGridlines.Border.ColorIndex = 5 'set color to blue
End If
End With
MajorTickMark returns or sets the type of major tick mark for the specified axis.
Charts("Chart1").Axes(xlValue).MajorTickMark = xlTickMarkOutside
MajorUnit returns or sets the major units for the value axis.
With Charts("Chart1").Axes(xlValue)
.MajorUnit = 100
.MinorUnit = 20
End With
MajorUnitIsAuto true if Microsoft Excel calculates the major units for the value axis.
With Charts("Chart1").Axes(xlValue)
.MajorUnitIsAuto = True
.MinorUnitIsAuto = True
End With
MajorUnitScale returns or sets the major unit scale value for the category axis when the CategoryType property is set to xlTimeScale.
With Charts(1).Axes(xlCategory)
.CategoryType = xlTimeScale
.MajorUnit = 5
.MajorUnitScale = xlDays
.MinorUnit = 1
.MinorUnitScale = xlDays
End With
MaximumScale returns or sets the maximum value on the value axis.
With Charts("Chart1").Axes(xlValue)
.MinimumScale = 10
.MaximumScale = 120
End With
MaximumScaleIsAuto true if Microsoft Excel calculates the maximum value for the value axis.
With Charts("Chart1").Axes(xlValue)
.MinimumScaleIsAuto = True
.MaximumScaleIsAuto = True
End With
MinimumScale returns or sets the minimum value on the value axis.
With Charts("Chart1").Axes(xlValue)
.MinimumScale = 10
.MaximumScale = 120
End With
MinimumScaleIsAuto true if Microsoft Excel calculates the minimum value for the value axis.
With Charts("Chart1").Axes(xlValue)
.MinimumScaleIsAuto = True
.MaximumScaleIsAuto = True
End With
MinorGridlines returns a Gridlines object that represents the minor gridlines for the specified axis. Only axes in the primary axis group can have gridlines.
With Charts("Chart1").Axes(xlValue)
If .HasMinorGridlines Then
.MinorGridlines.Border.ColorIndex = 5 'set color to blue
End If
End With
MinorTickMark returns or sets the type of minor tick mark for the specified axis.
Charts("Chart1").Axes(xlValue).MinorTickMark = xlTickMarkInside
MinorUnit returns or sets the minor units on the value axis.
With Charts("Chart1").Axes(xlValue)
.MajorUnit = 100
.MinorUnit = 20
End With
MinorUnitIsAuto true if Microsoft Excel calculates minor units for the value axis.
With Charts("Chart1").Axes(xlValue)
.MajorUnitIsAuto = True
.MinorUnitIsAuto = True
End With
MinorUnitScale returns or sets the minor unit scale value for the category axis when the CategoryType property is set to xlTimeScale.
With Charts(1).Axes(xlCategory)
.CategoryType = xlTimeScale
.MajorUnit = 5
.MajorUnitScale = xlDays
.MinorUnit = 1
.MinorUnitScale = xlDays
End With
Parent returns the parent object for the specified object. Read-only.
ReversePlotOrder true if Microsoft Excel plots data points from last to first.
Charts("Chart1").Axes(xlValue).ReversePlotOrder = True
ScaleType returns or sets the value axis scale type.
Charts("Chart1").Axes(xlValue).ScaleType = xlScaleLogarithmic
TickLabelPosition describes the position of tick-mark labels on the specified axis.
Charts("Chart1").Axes(xlCategory) _
.TickLabelPosition = xlTickLabelPositionHigh
TickLabels returns a TickLabels object that represents the tick-mark labels for the specified axis.
Charts("Chart1").Axes(xlValue).TickLabels.Font.ColorIndex = 3
TickLabelSpacing returns or sets the number of categories or series between tick-mark labels. Applies only to category and series axes. Can be a value from 1 through 31999.
Charts("Chart1").Axes(xlCategory).TickLabelSpacing = 10
TickLabelSpacingIsAuto returns or sets whether or not the tick label spacing is automatic.
TickMarkSpacing returns or sets the number of categories or series between tick marks. Applies only to category and series axes. Can be a value from 1 through 31999.
Charts("Chart1").Axes(xlCategory).TickMarkSpacing = 10
Top returns a Double value that represents the distance, in points, from the top edge of the object to the top of row 1 (on a worksheet) or the top of the chart area (on a chart).
Type returns an XlAxisType value that represents the Axis type.
Width returns a Double value that represents the width, in points, of the object.