Open In App

Programming Charts in Excel VBA

Last Updated : 11 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

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.

Clicking-file-option

 

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

Clicking-options-tab

 

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

Clicking-customize-ribbon

 

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:

Dataset

 

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.

Choosing-command-button

 

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.

VBA-editor-opens

 

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:

  • In the above code Lines from Private Sub CommandButton1_Click to End Sub defines the Click function on the button.
  • Dim refers to Dimension and it is used to declare variables in Excel VBA. 
  • Above we used variable bar_graph as type ChartObject. ChartObject contains all the sheets in the workbook (i.e, both chart sheets and worksheets).
  • We then set the ActiveSheet where the chart will be drawn such that its left and top corner is the E4 cell just to make sure the chart appears as close to the button and data table as possible.
  • The width and Height define the width and height of the chart that will be generated.
  • The chart is the function that helps create and change the type of chart in the ActiveSheet.
  • We set the source data for the chart with the help of SetSourceData and give it the data from Range A2 to C8 from Sheet1.
  • Using the ChartType function we can choose any kind of chart that we want to generate from the number of charts available in the list. The list of charts pops up automatically when one writes “Chart.ChartType =” in the VBA Editor.
  • The last line of code is optional and selects the A1 cell after completing the function which is just the Title of the table.

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. 

VBA-command

 

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.

Output-of-chart-function

 


Like Article
Suggest improvement
Share your thoughts in the comments

Similar Reads