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
Non-linear Components
In electrical circuits, Non-linear Components are electronic devices that need an external power source to operate actively. Non-Linear Components are those that are changed with respect to the voltage and current. Elements that do not follow ohm's law are called Non-linear Components. Non-linear Co
11 min read
Spring Boot Tutorial
Spring Boot is a Java framework that makes it easier to create and run Java applications. It simplifies the configuration and setup process, allowing developers to focus more on writing code for their applications. This Spring Boot Tutorial is a comprehensive guide that covers both basic and advance
10 min read
Class Diagram | Unified Modeling Language (UML)
A UML class diagram is a visual tool that represents the structure of a system by showing its classes, attributes, methods, and the relationships between them. It helps everyone involved in a projectâlike developers and designersâunderstand how the system is organized and how its components interact
12 min read
Steady State Response
In this article, we are going to discuss the steady-state response. We will see what is steady state response in Time domain analysis. We will then discuss some of the standard test signals used in finding the response of a response. We also discuss the first-order response for different signals. We
9 min read
Backpropagation in Neural Network
Back Propagation is also known as "Backward Propagation of Errors" is a method used to train neural network . Its goal is to reduce the difference between the modelâs predicted output and the actual output by adjusting the weights and biases in the network.It works iteratively to adjust weights and
9 min read
Polymorphism in Java
Polymorphism in Java is one of the core concepts in object-oriented programming (OOP) that allows objects to behave differently based on their specific class type. The word polymorphism means having many forms, and it comes from the Greek words poly (many) and morph (forms), this means one entity ca
7 min read
3-Phase Inverter
An inverter is a fundamental electrical device designed primarily for the conversion of direct current into alternating current . This versatile device , also known as a variable frequency drive , plays a vital role in a wide range of applications , including variable frequency drives and high power
13 min read
What is Vacuum Circuit Breaker?
A vacuum circuit breaker is a type of breaker that utilizes a vacuum as the medium to extinguish electrical arcs. Within this circuit breaker, there is a vacuum interrupter that houses the stationary and mobile contacts in a permanently sealed enclosure. When the contacts are separated in a high vac
13 min read
AVL Tree Data Structure
An AVL tree defined as a self-balancing Binary Search Tree (BST) where the difference between heights of left and right subtrees for any node cannot be more than one. The absolute difference between the heights of the left subtree and the right subtree for any node is known as the balance factor of
4 min read
CTE in SQL
In SQL, a Common Table Expression (CTE) is an essential tool for simplifying complex queries and making them more readable. By defining temporary result sets that can be referenced multiple times, a CTE in SQL allows developers to break down complicated logic into manageable parts. CTEs help with hi
6 min read