What Does the Number in the Addchart2 VBA Macro represent?
Last Updated : 14 Jun, 2023
A chart or graph can be made in an Excel spreadsheet by using the AddChart2 VBA macro. The desired chart type or style is indicated by the number in the AddChart2 VBA macro. The AddChart2 function uses this number as an input parameter to identify the type of chart or style to be constructed. Each chart type is given a different number. If change the argument 2 value, an area chart is represented by the number 1, a line chart by the number 3, and a pie chart by the number 5, similarly, formatting of charts can also be done if change the argument 1 value, data labels are added on the chart, by the number 202, dashed formatted chart is developed by the number 203. You can design a chart that displays your data in the most useful way for your purposes by providing the correct chart type.
Data types
Before moving forward, we need to learn about the various numerical data types, such as integers, decimals, or floating-point numbers, that may be part of the macro. The numbers in Addchart2 can be of any of the below-mentioned data types, hence it is necessary to know about them.
1. Integer: Using this data format, entire values can be represented between -32,768 and 32,767.
2. Long: The bigger whole numbers between -2,147,483,648 and 2,147,483,647 are represented by this data type.
3. Single: Single-precision, floating-point numbers, which have a precision of around 7 decimal digits, are represented by this data type.
4. Double: Precision till 15 decimal places, double-precision, floating-point numbers are represented by this data type.
5. Decimal: Accuracy of 28 decimal places, this data type is used to represent decimal values.
6. Alternative: Although this data type is more versatile and may represent any sort of data, including numerical data, it is less effective than utilizing a specialized data type.
AddChart2 Function and Numbers
AddChart2 function helps create any type of chart, in Excel, by VBA. It has total 7 arguments, out of which the first two arguments are very important. Their numbering represents the chart type and the type of formatting.
Syntax:
ActiveSheet.Shapes.AddChart2(Style, ChartType).Select
where,
Style = It is a number, which provides formatting to your chart,
ChartType = It can be text/number, which tells the type of chart to be shown.
For Example:
ActiveSheet.Shapes.AddChart2(203.5, 1).Select
where,
203.5 = Adds, a light gray shade to the background of chart,
1 = Adds, an Area chart in the active worksheet.
Code
Output
Why Formatting is represented in the Form of Numbers in AddChart2?
Charts have thousands of formatting types, which could be best represented in the form of numbers. The macro involves formatting numbers to show currency symbols or thousands of separators or rounding numerical data to a specific number of decimal places. In VBA, value describes the type of formatting, including the usage of currency symbols and the number of decimal places. This can be accomplished by either setting the NumberFormat property of a cell or range. Rounding helps better understand the formatting numbers, but rounding needs to be done carefully because it occasionally has unforeseen results or causes computations to be inaccurate.
List of ChartType in AddChart2
The macro might entail making graphs or charts to represent numerical data, including pie charts, bar charts, or line charts. To make a new chart and add it to a worksheet or chart sheet in VBA, use the "Addchart2" technique. To know the ChartType Microsoft has provided us with the list of numbers mapped with the chart type, which can be viewed in the documentation. Here, is the list of the most famous charts, and the number corresponding to them.
Value | Description |
1 | Area Chart |
5 | Pie Chart |
4 | Line Chart |
-4169 | Scatter Chart |
Note: It is important to note that, no such data has been provided for formatting numbers, because there could infinite of numbers, which could represent different formatting.
Error Handling and Precautions
The macro might entail dealing with errors that could occur when processing numerical data, like divide-by-zero or overflow issues. For handling errors, VBA includes a number of built-in methods and statements, including On Error GoTo, On Error Resume Next, and Err.Raise. With the aid of these tools, programmers may find and fix problems in their code, which can help avoid crashes and other strange behavior.
Generally, adding customized numbers, to the charts for formatting, is not a good option. VBA programmers, prefer, recording a macro, to create a chart, and then customizing it accordingly.
Similar Reads
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
How to Create a Pie of Pie Chart in Excel: Visualize Data with Two Data Sets A Pie Chart is one of the most popular data visualization tools used in Excel to display data in a circular form. However, when you have a lot of small values, they can be hard to differentiate in a standard pie chart. This is where the pie of pie chart with two data sets in Excel comes into play. I
7 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 pur
6 min read
How to Create a Macro in Excel: Build, Run, and Use Macros Easily How Do I Create a Macro in Excel- Quick StepsOpen MS Excel > Start Recording a MacroPerform the Actions You Want to Automate > Stop RecordingRun the MacroAre repetitive tasks consuming your time in Excel? With this Excel macros tutorial, you can learn how to create and use macros in Excel to a
8 min read
How to Create a Gantt Chart in Excel [Free Template] A Gantt chart in Excel is an essential tool for organizing and visualizing project timelines and milestones. This guide will show you how to create a Gantt chart in Excel using simple steps and a free Excel Gantt chart template, making it accessible for both beginners and professionals. Whether plan
6 min read