

#HOW TO PLOT A GRAPH IN EXCEL WITH TWO SETS OF DATA CODE#
While the following code may look acceptable, it will not work. ActiveWorkbook.Sheets("Sheet1").ChartObjects("Chart 1").Height = 300Įach item in the object hierarchy must be listed and separated by a period (. To change the height of Chart 1, on Sheet1, we could use the following. Therefore, to change a cell color to red, we would reference this as follows: ActiveWorkbook.Sheets("Sheet1").Range("A1").Interior.Color = RGB(255, 0, 0)Ĭharts are also part of the DOM and follow similar hierarchical principles. The Document Object Model (DOM) is a term which describes how things are structured.

By applying the principles and methods in this post, you will be able to do almost anything you want with charts in Excel using VBA. It is not feasible to provide code for every scenario you might come across there are just too many options. As a result, some of the code presented in this post may not work with versions before Excel 2013. For example, the AddChart2 method replaced the AddChart method. In Excel 2013, many changes were introduced to the charting engine and DOM. Understanding Excel’s Document Object Model (DOM) is essential to understand how VBA can be used with charts. While it might be tempting to skip straight to the section you need, I recommend you read the first section in full. But once you’ve mastered it, you’ll know the situations when VBA is the best option. The short code snippets below will help you apply some of the most common chart options with VBA (and hopefully turn those hours into minutes).īut don’t let me fool you into thinking that using VBA is quick and easy, it’s not. When we want to apply those hundreds of settings to lots of charts, it can take hours and hours of frustrating clicking. This is great for creating precisely the visualization we want but can be time-consuming to apply. Charts and graphs in Excel have hundreds of different options.
