{"id":2921,"date":"2017-10-11T02:21:25","date_gmt":"2017-10-11T02:21:25","guid":{"rendered":"http:\/\/excelvbatutor.com\/?page_id=2921"},"modified":"2020-04-23T10:58:53","modified_gmt":"2020-04-23T10:58:53","slug":"excel-vba-lesson-24-creating-charts-graphs-excel-vba","status":"publish","type":"page","link":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-24-creating-charts-graphs-excel-vba\/","title":{"rendered":"Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA"},"content":{"rendered":"\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-23-listbox-combobox\/\">&lt;&lt;Lesson 23&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-25-animation\/\">&lt;&lt;Lesson 25&gt;&gt;<\/a><\/strong><\/h4>\n\n\n\n<p>In Excel VBA, we can write code to create charts and graphs easily based on the data on the spreadsheet. Excel VBA has made charting engine as part of the Shape object. It is also an object by itself. We can create charts on a sheet of their own or embed them into an existing worksheet. The chart sheet is the Chart object whereas the embedded chart is part of the shape collection for the worksheet.<\/p>\n\n\n\n<h3 class=\"wp-block-heading\">24.1 Creating a Pie Chart<\/h3>\n\n\n\n<p>To create a pie chart in a spreadsheet, first of all, you need to enter a range of data in a spreadsheet. After entering the data, you need to name the range by right-clicking the range and clicking define the name in the pop-up menu. Name the chart MyChart<\/p>\n\n\n\n<p>Now insert a command button and then click it to enter the following code in the Excel VBA editor<\/p>\n\n\n\n<p>Private Sub CommandButton1_Click()<\/p>\n\n\n\n<p>ActiveSheet.Shapes.AddChart.Select<br>\nActiveSheet.Shapes(1).Top = 10<br>\nActiveSheet.Shapes(1).Left = 10<br>\nActiveChart.ChartType = xl3DPie<br>\nActiveChart.PlotArea.Select<br>\nActiveChart.SetSourceData Source:=Range(&#8220;MyChart&#8221;)<br>\nActiveChart.HasTitle = True<br>\nActiveChart.ChartTitle.Text = &#8220;My Chart&#8221;<\/p>\n\n\n\n<p>End Sub<\/p>\n\n\n\n<p><script async=\"\" src=\"\/\/pagead2.googlesyndication.com\/pagead\/js\/adsbygoogle.js\"><\/script><br>\n<ins class=\"adsbygoogle\" style=\"display: block; text-align: center;\" data-ad-layout=\"in-article\" data-ad-format=\"fluid\" data-ad-client=\"ca-pub-3033628290023372\" data-ad-slot=\"9639157585\"><\/ins><br>\n<script><br \/>\n     (adsbygoogle = window.adsbygoogle || []).push({});<br \/>\n<\/script><\/p>\n\n\n\n<h4 class=\"wp-block-heading\">The Output<\/h4>\n\n\n\n<div class=\"wp-block-image\"><figure class=\"aligncenter\"><a href=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_Figure24.1.jpg\"><img loading=\"lazy\" decoding=\"async\" width=\"806\" height=\"591\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_Figure24.1.jpg\" alt=\"vba2010_Figure24.1\" class=\"wp-image-1508\"\/><\/a><\/figure><\/div>\n\n\n\n<h4 class=\"wp-block-heading\">Figure 24.1: Pie Chart<\/h4>\n\n\n\n<p>To separate the pie chart into slices (The technical term is explode), you can add this line to the code:<\/p>\n\n\n\n<p>ActiveChart.SeriesCollection(1).Explosion = 10<\/p>\n\n\n\n<p>As there are five sectors, setting the explosion value to 10 explode all slices. You will get the following chart:\n<\/p>\n\n\n\n<figure class=\"wp-block-image\"><img decoding=\"async\" src=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_figure24.4.jpg\" alt=\"\"\/><\/figure>\n\n\n\n<h4 class=\"wp-block-heading\">Figure 24.2<\/h4>\n\n\n\n<h3 class=\"wp-block-heading\">24.2 Creating a Bar Graph<\/h3>\n\n\n\n<p>To draw a bar graph, you just need to change the ChartType to xl3DColumn.<\/p>\n\n\n\n<p>Private Sub CommandButton2_Click()<\/p>\n\n\n\n<p>ActiveSheet.Shapes.AddChart.Select<br>\nActiveSheet.Shapes(1).Top = 10<br>\nActiveSheet.Shapes(1).Left = 10<br>\nActiveChart.ChartType = xl3DColumn<br>\nActiveChart.PlotArea.Select<br>\nActiveChart.SetSourceData Source:=Range(&#8220;MyChart&#8221;)<br>\nActiveChart.HasTitle = True<br>\nActiveChart.ChartTitle.Text = &#8220;My Chart&#8221;<\/p>\n\n\n\n<p>End Sub<br>\n<\/p>\n\n\n\n<h4 class=\"wp-block-heading\"><strong><a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-23-listbox-combobox\/\">&lt;&lt;Lesson 23&gt;&gt;<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/tutorial\/\">[Contents]<\/a> <a href=\"http:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-25-animation\/\">&lt;&lt;Lesson 25&gt;&gt;<\/a><\/strong><\/h4>\n","protected":false},"excerpt":{"rendered":"<p>&lt;&lt;Lesson 23&gt;&gt; [Contents] &lt;&lt;Lesson 25&gt;&gt; In Excel VBA, we can write code to create charts and graphs easily based on the data on the spreadsheet. Excel VBA has made charting engine as part of the Shape object. It is also an object by itself. We can create charts on a sheet of their own or &hellip; <a href=\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-24-creating-charts-graphs-excel-vba\/\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA&#8221;<\/span><\/a><\/p>\n","protected":false},"author":5012,"featured_media":0,"parent":0,"menu_order":0,"comment_status":"closed","ping_status":"closed","template":"","meta":{"footnotes":""},"categories":[12],"tags":[],"class_list":["post-2921","page","type-page","status-publish","hentry","category-charts"],"yoast_head":"<!-- This site is optimized with the Yoast SEO plugin v27.1.1 - https:\/\/yoast.com\/product\/yoast-seo-wordpress\/ -->\n<title>Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor<\/title>\n<meta name=\"robots\" content=\"index, follow, max-snippet:-1, max-image-preview:large, max-video-preview:-1\" \/>\n<link rel=\"canonical\" href=\"https:\/\/excelvbatutor.com\/vba_lesson24.htm\" \/>\n<meta property=\"og:locale\" content=\"en_US\" \/>\n<meta property=\"og:type\" content=\"article\" \/>\n<meta property=\"og:title\" content=\"Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"og:description\" content=\"&lt;&lt;Lesson 23&gt;&gt; [Contents] &lt;&lt;Lesson 25&gt;&gt; In Excel VBA, we can write code to create charts and graphs easily based on the data on the spreadsheet. Excel VBA has made charting engine as part of the Shape object. It is also an object by itself. We can create charts on a sheet of their own or &hellip; Continue reading &quot;Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA&quot;\" \/>\n<meta property=\"og:url\" content=\"https:\/\/excelvbatutor.com\/vba_lesson24.htm\" \/>\n<meta property=\"og:site_name\" content=\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\" \/>\n<meta property=\"article:modified_time\" content=\"2020-04-23T10:58:53+00:00\" \/>\n<meta property=\"og:image\" content=\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_Figure24.1.jpg\" \/>\n\t<meta property=\"og:image:width\" content=\"806\" \/>\n\t<meta property=\"og:image:height\" content=\"591\" \/>\n\t<meta property=\"og:image:type\" content=\"image\/jpeg\" \/>\n<meta name=\"twitter:label1\" content=\"Est. reading time\" \/>\n\t<meta name=\"twitter:data1\" content=\"2 minutes\" \/>\n<script type=\"application\/ld+json\" class=\"yoast-schema-graph\">{\"@context\":\"https:\/\/schema.org\",\"@graph\":[{\"@type\":\"WebPage\",\"@id\":\"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-24-creating-charts-graphs-excel-vba\/\",\"url\":\"https:\/\/excelvbatutor.com\/vba_lesson24.htm\",\"name\":\"Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"isPartOf\":{\"@id\":\"https:\/\/excelvbatutor.com\/#website\"},\"primaryImageOfPage\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson24.htm#primaryimage\"},\"image\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson24.htm#primaryimage\"},\"thumbnailUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_Figure24.1.jpg\",\"datePublished\":\"2017-10-11T02:21:25+00:00\",\"dateModified\":\"2020-04-23T10:58:53+00:00\",\"breadcrumb\":{\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson24.htm#breadcrumb\"},\"inLanguage\":\"en-US\",\"potentialAction\":[{\"@type\":\"ReadAction\",\"target\":[\"https:\/\/excelvbatutor.com\/vba_lesson24.htm\"]}]},{\"@type\":\"ImageObject\",\"inLanguage\":\"en-US\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson24.htm#primaryimage\",\"url\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_Figure24.1.jpg\",\"contentUrl\":\"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_Figure24.1.jpg\",\"width\":806,\"height\":591},{\"@type\":\"BreadcrumbList\",\"@id\":\"https:\/\/excelvbatutor.com\/vba_lesson24.htm#breadcrumb\",\"itemListElement\":[{\"@type\":\"ListItem\",\"position\":1,\"name\":\"Home\",\"item\":\"https:\/\/excelvbatutor.com\/\"},{\"@type\":\"ListItem\",\"position\":2,\"name\":\"Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA\"}]},{\"@type\":\"WebSite\",\"@id\":\"https:\/\/excelvbatutor.com\/#website\",\"url\":\"https:\/\/excelvbatutor.com\/\",\"name\":\"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor\",\"description\":\"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.\",\"inLanguage\":\"en-US\"}]}<\/script>\n<!-- \/ Yoast SEO plugin. -->","yoast_head_json":{"title":"Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","robots":{"index":"index","follow":"follow","max-snippet":"max-snippet:-1","max-image-preview":"max-image-preview:large","max-video-preview":"max-video-preview:-1"},"canonical":"https:\/\/excelvbatutor.com\/vba_lesson24.htm","og_locale":"en_US","og_type":"article","og_title":"Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","og_description":"&lt;&lt;Lesson 23&gt;&gt; [Contents] &lt;&lt;Lesson 25&gt;&gt; In Excel VBA, we can write code to create charts and graphs easily based on the data on the spreadsheet. Excel VBA has made charting engine as part of the Shape object. It is also an object by itself. We can create charts on a sheet of their own or &hellip; Continue reading \"Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA\"","og_url":"https:\/\/excelvbatutor.com\/vba_lesson24.htm","og_site_name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","article_modified_time":"2020-04-23T10:58:53+00:00","og_image":[{"width":806,"height":591,"url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_Figure24.1.jpg","type":"image\/jpeg"}],"twitter_misc":{"Est. reading time":"2 minutes"},"schema":{"@context":"https:\/\/schema.org","@graph":[{"@type":"WebPage","@id":"https:\/\/excelvbatutor.com\/index.php\/excel-vba-lesson-24-creating-charts-graphs-excel-vba\/","url":"https:\/\/excelvbatutor.com\/vba_lesson24.htm","name":"Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA - Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","isPartOf":{"@id":"https:\/\/excelvbatutor.com\/#website"},"primaryImageOfPage":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson24.htm#primaryimage"},"image":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson24.htm#primaryimage"},"thumbnailUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_Figure24.1.jpg","datePublished":"2017-10-11T02:21:25+00:00","dateModified":"2020-04-23T10:58:53+00:00","breadcrumb":{"@id":"https:\/\/excelvbatutor.com\/vba_lesson24.htm#breadcrumb"},"inLanguage":"en-US","potentialAction":[{"@type":"ReadAction","target":["https:\/\/excelvbatutor.com\/vba_lesson24.htm"]}]},{"@type":"ImageObject","inLanguage":"en-US","@id":"https:\/\/excelvbatutor.com\/vba_lesson24.htm#primaryimage","url":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_Figure24.1.jpg","contentUrl":"https:\/\/excelvbatutor.com\/wp-content\/uploads\/2013\/12\/vba2010_Figure24.1.jpg","width":806,"height":591},{"@type":"BreadcrumbList","@id":"https:\/\/excelvbatutor.com\/vba_lesson24.htm#breadcrumb","itemListElement":[{"@type":"ListItem","position":1,"name":"Home","item":"https:\/\/excelvbatutor.com\/"},{"@type":"ListItem","position":2,"name":"Excel VBA Lesson 24: Creating Charts and Graphs in Excel VBA"}]},{"@type":"WebSite","@id":"https:\/\/excelvbatutor.com\/#website","url":"https:\/\/excelvbatutor.com\/","name":"Learn Excel VBA Online \u2013 Step-by-Step Tutorials &amp; Courses | ExcelVBATutor","description":"Master Excel VBA with free tutorials, examples, and personalized guidance. Perfect for beginners and advanced users looking to automate Excel.","inLanguage":"en-US"}]}},"_links":{"self":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/2921","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages"}],"about":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/types\/page"}],"author":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/users\/5012"}],"replies":[{"embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/comments?post=2921"}],"version-history":[{"count":7,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/2921\/revisions"}],"predecessor-version":[{"id":3468,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/pages\/2921\/revisions\/3468"}],"wp:attachment":[{"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/media?parent=2921"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/categories?post=2921"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/excelvbatutor.com\/index.php\/wp-json\/wp\/v2\/tags?post=2921"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}