Open In App

Programming Charts in Excel VBA

VBA stands for Visual Basic for Applications and it is developed by Microsoft. MS Excel and many other Microsoft applications like word, access, etc have this language integrated into them using which one can do various things. VBA can be used in MS Excel through the code editor in its developer tab through which one can generate various types of charts and user-defined functions, connect to windows APIs, and much more. 

Enabling the Developer Tab in Excel

To use VBA in excel first of all we will need to make the developer tab visible as it is not visible as default when it is installed. We can do this by following the steps below:



Step 1: Click on the file option in the top left corner.

 

Step 2: Click on the options tab in the bottom left corner which will take you to excel options.



 

Step 3: Then click on the customize ribbon option and check the developer tab in the options available.

 

Table of Data for Charts

The table of data that I used for generating the chart is a small table containing the marks of students in different subjects. The table is shown below:

 

Programming Charts in Excel VBA

To produce charts from the data table present in our sheet first we need to create a command button by clicking which we will generate the desired chart that we programmed in the VBA. To do so just select the developer tab and then select insert then from ActiveX Controls choose the command button then place it anywhere in the sheet.

 

After placing the button in the sheet double click the button, if it doesn’t work then click the design mode option in the developer tab and then double-click on the button. It will take you to the VBA Editor where it will open starting with the function that handles the click function of the button you just created.

 

To increase the font size in the VBA Editor go to the tools tab and then the options tab and then to the editor format tab and increase the font size to your desired size. After we got our button and adjusted the font size its time to write some programs to create charts using those buttons and data tables in the sheet. Below is a VBA Code that runs when the user clicks the command Button. Remember to change to design mode to go to the editor when you click the button and turn off the design mode when you want to see the click function in action.

Private Sub CommandButton1_Click()

Dim bar_graph As ChartObject

Set bar_graph = ActiveSheet.ChartObjects.Add(Top:=Range(“E4”).Top, Left:=Range(“E4”).Left, Width:=400, Height:=300)

bar_graph.Chart.SetSourceData Worksheets(“Sheet1”).Range(“A2:C8”)

bar_graph.Chart.ChartType = xl3DColumn // Here you can choose from a variety of chart types

Worksheets(“Sheet1”).Cells(1, 1).Select // Optional

End Sub

Let’s Understand the code written above in Excel VBA:

There are many other ways to write the same code in Excel VBA and I have shown just a single way. With the help of variables for every function and using the keyword to change their attributes can be learned easily if one learns how to write VBA in a better way. 

 

Output of Chart Function

When the command button is pressed outside the design mode, the chosen chart is displayed in the cell range mentioned in the code above like the picture shown below.

 

Article Tags :