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.
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.
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
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.
You can refer the following link for the step by step instructions.
Click on the following links of the useful resources. These helps to learn and gain more knowledge.