Automate Chart Creation Using Excel VBA (Macros)
A question from a website visitor: I need few examples to create macros in one Excel sheet tab to form graph or chart in next sheet tab of same excel workbook. Could you please help me to achieve same through EXCEL MACROS? Thanks in advance.
From the developer tab go to the controls group and click on the down arrow of ‘insert’ and from the Activex controls select a command button. Click and drag to create a command button under the data.
Click on the ‘Design Mode’ in the controls group and then click the command button to select it and finally the ‘properties’ option in the ‘controls’ group. Now change the caption of the command button to ‘create chart’.
Next click on ‘view code’ also in the controls group of the developer tab and add the following code between the ‘sub’ and ‘end sub’ and your final code will look like this:
Private Sub CommandButton1_Click()
‘ First we select the names of the students along with the header ‘Name of student’ and the Average Marks also with the header
‘In the active sheet we add a chart
‘ We set the source data for the chart
ActiveChart.SetSourceData Source:=Range( _
‘We define the type of chart
ActiveChart.ChartType = xlColumnClustered
‘ Before we can perform an action on the chart we need to activate it
‘We perform the cut operation
‘we select the Sheet2 where we wish to paste our chart
‘We now paste the chart in the Sheet2 whic has become the active sheet after selection
‘we return to sheet1
‘ we select the cell F9 in sheet1
The code to decide Grades of the students is given here for reference:
Watch the training video below to learn how to automate the creation of charts using macros (VBA) in Microsoft Excel: