VBA Add Chart Name to specific or active Chart in Excel. 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 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.
Instructions to Run VBA Macro Code or Procedure:
You can refer the following link for the step by step instructions.
Instructions to run VBA Macro Code
Other Useful Resources:
Click on the following links of the useful resources. These helps to learn and gain more knowledge.
VBA Tutorial VBA Functions List VBA Arrays VBA Text Files VBA Tables
VBA Editor Keyboard Shortcut Keys List VBA Interview Questions & Answers Blog