VBA for Excel 2007 tutorial-axes titles of chart


Axes titles of chart

In this VBA code example, you will learn VBA code to display the titles of horizontal and vertical axess of a chart. Setting the horizontal and vertical axes of the chart can be accomplished by using the Axes() method. This method returns an axis object that contains AxisTitle object with which we can set the titles of axes. It has the following prototype:


ActiveChart.Axes(Type)

The Type argument represents type of axis. It can be xlValue (horizental axis) and xlCategory (Vertical axis).




VBA for Excel: VBA example code to set chart axes

To run this VBA example code, you can copy and paste the VBA code below in to the block of Worksheet_Activate() procedure. To open this block, double-click the current worksheet, then in the objects dropdown list select Sheet1 and in events list select Activate.



VBA code for this example:


Option Explicit
Private Sub Worksheet_Activate()   
 
    'Select the data source
    Range("A1:G3").Select
    'Create a clusted column chart
    ActiveSheet.Shapes.AddChart xl3DColumnClustered, 10, 100
    ActiveSheet.Shapes(1).Select
    ActiveChart.SetSourceData Source:=Range("Sheet1!$A$1:$G$3"), PlotBy:=xlRows   
   
    'Set chart title
    ActiveChart.HasTitle = True
    ActiveChart.ChartTitle.Caption = "Sale by Year"
    'Set vertical axis title and display it in 30 degree direction
    With ActiveChart.Axes(xlCategory)
    .HasTitle = True
    .AxisTitle.Text = "Year"
    .AxisTitle.Orientation = 30   
    End With

    'Set horizontal axis title and display it in 90 degree direction
    With ActiveChart.Axes(xlValue)
    .HasTitle = True
    .AxisTitle.Text = "Quantity"
    .AxisTitle.Orientation = 90
   
    End With      
 
End Sub

Comments





This website intents to provide free and high quality tutorials, examples, exercises and solutions, questions and answers of programming and scripting languages:
C, C++, C#, Java, VB.NET, Python, VBA,PHP & Mysql, SQL, JSP, ASP.NET,HTML, CSS, JQuery, JavaScript and other applications such as MS Excel, MS Access, and MS Word. However, we don't guarantee all things of the web are accurate. If you find any error, please report it then we will take actions to correct it as soon as possible.