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

 


Similar Reads

VBA Subroutine in Excel - How to Call Sub in VBA?
When a specified action is performed on a worksheet with the help of a collection of code known as a VBA Subroutine. It also helps to read an external file, also it can open other applications from Excel. A large piece of code can be broken into small parts so that we can manage it easily. Let's learn why to use submarines: Converts large piece of
3 min read
How to Create Charts in Excel Using Worksheet Data and VBA?
Excel is an important software provided by Microsoft Corporation. This software belongs to one of the major software suites Office 365. In this software suite, there are other software are present like Word, PowerPoint, etc. They are called Office 365, as this software are mostly used for office purpose. But now the world has changed a lot. After t
7 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 following is an illustration of a graph that utili
2 min read
Excel VBA | count() functions
Visual Basic for Applications (VBA) is the programming language of Excel and other offices. It is an event-driven programming language from Microsoft. With Excel VBA one can automate many tasks in excel and all other office software. It helps in generating reports, preparing various charts, graphs and moreover, it performs calculation using its var
2 min read
How to Use for Each Loop in Excel VBA?
A For Each loop is used to execute a statement or a set of statements for each element in an array or collection. Syntax: For Each element In group [ statements ] [ Exit For ] [ statements ] Next [ element ] The For...Each...Next statement syntax has the following three parts: PartDescriptionelement Required (Must be mentioned). Variable is used to
3 min read
Excel VBA | sum() functions
Visual Basic for Applications (VBA) is the programming language of Excel and other offices. It is an event-driven programming language from Microsoft. With Excel VBA one can automate many tasks in excel and all other office software. It helps in generating reports, preparing various charts, graphs and moreover, it performs calculation using its var
2 min read
Excel VBA | Average() Functions
VBA (Visual Basic for Applications) is the programming language of Excel and other offices. It is an event-driven programming language from Microsoft. With Excel VBA, one can automate many tasks in excel and all other office softwares. It helps in generating reports, preparing various charts, graphs and moreover, it performs calculation using its v
2 min read
How to Declare and Initialize String Array in Excel VBA?
A string array is an array where we can store only string values in the array, with the help of a string array, we can store more than one string value. We can declare the string array in many ways like declaring a static string array, declaring a variant size array of string using the Array function, and a string array using the split function whi
2 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 that dynamic ranges are used. We can trap dynamic
3 min read
VBA Strings in Excel
In the realm of Excel's Visual Basic for Applications(VBA), strings play a pivotal role 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 handles strings is essential for
8 min read
Article Tags :