Programming Charts in Excel VBA
Last Updated :
11 Nov, 2022
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:
- 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.
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.

Similar Reads
VBA Strings in Excel
In Excel's Visual Basic for Applications(VBA), strings are pivotal in handling and manipulating text-based data. Strings serve as a fundamental data type used to store a sequence of characters, enabling the representation of textual information, numbers, symbols, and more. Understanding how VBA hand
8 min read
Trapping Dynamic Ranges in Excel VBA
Dynamic Ranges give the flexibility to work with more range of cells as compared to a specific range of cells which usually limits us to work with a particular range of cells. As the size of the data may change in an excel sheet so it is also necessary to change the range in the code dynamically for
3 min read
VBA Arithmetic Operators in Excel
Arithmetic Operators are the operators which perform mathematical operation or which perform any operation between two numbers like addition(+), subtraction(-), multiplication(*), division( / ), exponentiation(^), modulus(Mod), Bit-Shift operations. In this article, we will learn about the excel VBA
3 min read
Change value in Excel using Python
In this article, We are going to change the value in an Excel Spreadsheet using Python. Method 1: Using openxml: openpyxl is a Python library to read/write Excel xlsx/xlsm/xltx/xltm files. It was born from a lack of an existing library to read/write natively from Python the Office Open XML format. o
2 min read
VBA Arrays in Excel
Arrays are used to store data of similar data types. Suppose there are 300 students rather than declaring 300 variables for students, we can declare one array where we can store 300 elements. In this article, we will learn about excel VBA arrays. Declaration of Array Declaring an array is similar to
5 min read
VBA Constants in Excel
There can be situations when you want to declare a number in your program that you never want to be changed. In other situations, like, as if you have used a person's name many times in your program and you want to change that name, this assigned task could be hectic if you change the name of that p
6 min read
Multidimensional Arrays in Excel VBA
Multidimensional Arrays are used to store data of similar data types of more than one dimension. A multidimensional array has a dimension up to 60 but usually, we don't use arrays of dimensions more than 3 or 4. Here, we will see how to declare a multidimensional array in many ways and also how to c
2 min read
Excel VBA Comparison Operators
VBA in Excel stands for Visual Basic for Applications which is Microsoft's programming language. To optimize the performance and reduce the time in Excel we need Macros and VBA is the tool used in the backend. Some helpful links to get more insights about Macros, VBA in Excel : 1. Record Macros in E
5 min read
Generating Dynamic Charts With VBA in Excel
A powerful graph range is an information range that refreshes naturally when you change the information source. This unique reach is then utilized as the source information in an outline. As the information changes, the powerful reach refreshes right away which prompts an update in the outline. The
2 min read
VBA Print Statement in Excel
When there are many columns and rows in excel but we only want to save or print a few of them because that few rows and columns are important to us, at that case we will print them in Excel VBA so that we can fetch those details later and also to get the print out of that table because daily life ha
2 min read