Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Creating Charts in VBA.
Written by Allen Wyatt (last updated February 22, 2020)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365
Excel is very handy at creating charts from data in a worksheet. What if you want to create a chart directly from VBA, without using any data in a worksheet? You can do this by "fooling" Excel into thinking it is working with information from a worksheet, and then providing your own. The following macro illustrates this concept:
Sub MakeChart() 'Add a new chart Charts.Add 'Set the dummy data range for the chart ActiveChart.SetSourceData Sheets("Sheet1").Range("a1:d4"), _ PlotBy:=xlColumns 'Manually set the values for the data series ActiveChart.SeriesCollection(1).Formula = _ "=SERIES(""First Data"",{""a"",""b"",""c"",""d""},{2,3,4,5},1)" ActiveChart.SeriesCollection(2).Formula = _ "=SERIES(""Second Data"",{""a"",""b"",""c"",""d""},{6,7,8,9},2)" ActiveChart.SeriesCollection(3).Formula = _ "=SERIES(""Third Data"",{""a"",""b"",""c"",""d""},{10,11,12,13},3)" End Sub
The comments in this example explain what is going on for each step. When setting the dummy data range, the SetSourceData method assumes the range is on a worksheet named Sheet1. If you don't have such a sheet in your workbook, you need to alter the command accordingly.
Later, when manually setting the values for the data series, the SERIES command is used to specify the label for the series (First Data, Second Data, and Third Data), the array of category labels (a, b, c, and d in all series), the array of values for the series, and a number specifying which series number this represents.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (10347) applies to Microsoft Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365. You can find a version of this tip for the older menu interface of Excel here: Creating Charts in VBA.
Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!
Excel can display both values and names for data points in a chart, when you hover the mouse over the data point. This ...
Discover MoreYou can create hyperlinks to all sorts of worksheets in a workbook, but you cannot create a hyperlink to a chart sheet. ...
Discover MoreCharts can either be embedded in a worksheet or take up an entire sheet by themselves. Changing from one type of chart to ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
There are currently no comments for this tip. (Be the first to leave your comment—just use the simple form above!)
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2024 Sharon Parq Associates, Inc.
Comments