Skip to content
geeksforgeeks
  • Tutorials
    • Python
    • Java
    • Data Structures & Algorithms
    • ML & Data Science
    • Interview Corner
    • Programming Languages
    • Web Development
    • CS Subjects
    • DevOps And Linux
    • School Learning
    • Practice Coding Problems
  • Courses
    • DSA to Development
    • Get IBM Certification
    • Newly Launched!
      • Master Django Framework
      • Become AWS Certified
    • For Working Professionals
      • Interview 101: DSA & System Design
      • Data Science Training Program
      • JAVA Backend Development (Live)
      • DevOps Engineering (LIVE)
      • Data Structures & Algorithms in Python
    • For Students
      • Placement Preparation Course
      • Data Science (Live)
      • Data Structure & Algorithm-Self Paced (C++/JAVA)
      • Master Competitive Programming (Live)
      • Full Stack Development with React & Node JS (Live)
    • Full Stack Development
    • Data Science Program
    • All Courses
  • Excel Tutorial
  • Excel Formulas
  • Excel Shortcut Keys
  • Data Analysis in Excel
  • Formatting in Excel
  • Excel Workbooks
  • Statistical Functions
  • Data Visualization in Excel
  • Pivot Tables in Excel
  • MS Excel Quiz
  • Excel Interview Questions
  • Advance Excel
Open In App
Next Article:
What Does the Number in the Addchart2 VBA Macro represent?
Next article icon

What Does the Number in the Addchart2 VBA Macro represent?

Last Updated : 14 Jun, 2023
Comments
Improve
Suggest changes
Like Article
Like
Report

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

Working code for VBA using AddChart2
 

Output 

Output of the Code for AddChart2 using VBA
 

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. 

ValueDescription
1Area Chart
5Pie Chart
4 Line Chart
-4169Scatter 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. 


Next Article
What Does the Number in the Addchart2 VBA Macro represent?

S

sudarshandixit29
Improve
Article Tags :
  • Excel
  • Excel-VBA

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
geeksforgeeks-footer-logo
Corporate & Communications Address:
A-143, 7th Floor, Sovereign Corporate Tower, Sector- 136, Noida, Uttar Pradesh (201305)
Registered Address:
K 061, Tower K, Gulshan Vivante Apartment, Sector 137, Noida, Gautam Buddh Nagar, Uttar Pradesh, 201305
GFG App on Play Store GFG App on App Store
Advertise with us
  • Company
  • About Us
  • Legal
  • Privacy Policy
  • In Media
  • Contact Us
  • Advertise with us
  • GFG Corporate Solution
  • Placement Training Program
  • Languages
  • Python
  • Java
  • C++
  • PHP
  • GoLang
  • SQL
  • R Language
  • Android Tutorial
  • Tutorials Archive
  • DSA
  • Data Structures
  • Algorithms
  • DSA for Beginners
  • Basic DSA Problems
  • DSA Roadmap
  • Top 100 DSA Interview Problems
  • DSA Roadmap by Sandeep Jain
  • All Cheat Sheets
  • Data Science & ML
  • Data Science With Python
  • Data Science For Beginner
  • Machine Learning
  • ML Maths
  • Data Visualisation
  • Pandas
  • NumPy
  • NLP
  • Deep Learning
  • Web Technologies
  • HTML
  • CSS
  • JavaScript
  • TypeScript
  • ReactJS
  • NextJS
  • Bootstrap
  • Web Design
  • Python Tutorial
  • Python Programming Examples
  • Python Projects
  • Python Tkinter
  • Python Web Scraping
  • OpenCV Tutorial
  • Python Interview Question
  • Django
  • Computer Science
  • Operating Systems
  • Computer Network
  • Database Management System
  • Software Engineering
  • Digital Logic Design
  • Engineering Maths
  • Software Development
  • Software Testing
  • DevOps
  • Git
  • Linux
  • AWS
  • Docker
  • Kubernetes
  • Azure
  • GCP
  • DevOps Roadmap
  • System Design
  • High Level Design
  • Low Level Design
  • UML Diagrams
  • Interview Guide
  • Design Patterns
  • OOAD
  • System Design Bootcamp
  • Interview Questions
  • Inteview Preparation
  • Competitive Programming
  • Top DS or Algo for CP
  • Company-Wise Recruitment Process
  • Company-Wise Preparation
  • Aptitude Preparation
  • Puzzles
  • School Subjects
  • Mathematics
  • Physics
  • Chemistry
  • Biology
  • Social Science
  • English Grammar
  • Commerce
  • World GK
  • GeeksforGeeks Videos
  • DSA
  • Python
  • Java
  • C++
  • Web Development
  • Data Science
  • CS Subjects
@GeeksforGeeks, Sanchhaya Education Private Limited, All rights reserved
We use cookies to ensure you have the best browsing experience on our website. By using our site, you acknowledge that you have read and understood our Cookie Policy & Privacy Policy
Lightbox
Improvement
Suggest Changes
Help us improve. Share your suggestions to enhance the article. Contribute your expertise and make a difference in the GeeksforGeeks portal.
geeksforgeeks-suggest-icon
Create Improvement
Enhance the article with your expertise. Contribute to the GeeksforGeeks community and help create better learning resources for all.
geeksforgeeks-improvement-icon
Suggest Changes
min 4 words, max Words Limit:1000

Thank You!

Your suggestions are valuable to us.

What kind of Experience do you want to share?

Interview Experiences
Admission Experiences
Career Journeys
Work Experiences
Campus Experiences
Competitive Exam Experiences