Add Chart Title using VBA in Excel

Add Chart Title

Add Chart Name to specific or active Chart in Excel using VBA. In this tutorial we learn how to add title to the chart with examples and step by step instructions.

Add Title to Chart in a Excel Workbook using VBA

Here is a VBA code to add title to the chart in Excel Workbook. In the below code ‘sChartName’ represents a chart name. And “Charts” is a Worksheet name. finally ‘ChartObjects(1)’ represents the first chart in the ‘Charts’ worksheet.

'Add Title to Chart using VBA
Sub VBAF1_Add_Chart_Title()
    
    'declare a variable
    Dim sChartName As String
    
    'Assign chart name to variable
    sChartName = "Region Sales Data"
    
    'Assign title to a chart
    With ActiveWorkbook.Sheets("Charts").ChartObjects(1).Chart
        .HasTitle = True
        .ChartTitle.Text = sChartName
    End With

End Sub

Output: You can see output as shown in below screenshot.

Add Chart Title
Add Chart Title

Add Name of the Chart to Active Chart using VBA in a Excel Workbook

We can add title to active chart using below VBA code or procedure.

'Add Chart Name to Active Chart
Sub VBAF1_Chart_Name_to_Active_Chart()
    
    Dim sChartName As String
    
    sChartName = "Region Sales Data"
    
    'Assign title to the chart
    With ActiveChart
        .SetElement (msoElementChartTitleAboveChart)
        .ChartTitle.Text = sChartName
    End With
    
End Sub

Get Name of the Chart from the user Input and Add to Chart using VBA

Let us see how to get dynamic chart title from the user input. Once we get title of the chart we add name of the chart to chart in a workbook.

'Chart Name from the User Input to Active Chart
Sub VBAF1_Chart_Title_From_User_Input()
    
    Dim sChartName As String
    
    'Title of the chart should be string data as we declared as string datatype
    sChartName = InputBox("Enter Chart Title", "Title of the Chart")
    
    'Assign title to the chart
    With ActiveChart
        .SetElement (msoElementChartTitleAboveChart)
        .ChartTitle.Text = sChartName
    End With
    
End Sub

Output: Here is a User Input screenshot for your reference.

Chart Name from the user input
Chart Name from the User Input

Instructions to run macro or procedure:

Here are the step by step instructions to use above macro codes in Visual basic editor.

  • Open Visual Basic Editor(VBE) or you can click Alt +F11
  • Go to VBA code window or you can click F7
  • Copy above macro code or procedure
  • Paste above code in code window
  • Run macro by clicking F5 or Run command
  • The output of the above macro procedures shown in output screenshots above.

Other Useful Resources

Click on the following links of the useful resources. These helps to learn and gain more knowledge.

Charts Tutorial VBA Tutorial VBA Functions List Blog

Other related References

Excel Chart VBA Examples and Tutorials

Leave a Comment