vba set source data in Excel

Q178. In Excel, how can I set the Source Data of charts using VBA?

For example, I want to dynamically specify the data range for my chart using VBA.

To do it in Excel, here is the answer:

  1. Option Explicit
  2. Sub SetChartSourceData()
  3. ActiveSheet.ChartObjects("Chart1").Chart.SetSourceData Source:=ActiveSheet.Range("$A$1:$B$10")
  4. End Sub


a) Line 3 - Sets the source data range for "Chart1" to "$A$1:$B$10".

b) If the data is non-contiguous (say X-Axis labels are in column A and Y-Axis values to be plotted are in column D), the 2 ranges have to be specified separately seperated by a comma as in "$A$1:$A$10,$D$1:$D$10".


You can find similar Excel Questions and Answer hereunder

1) How do I update my DropDown list whenever the sheet is activated?

2) How do I copy a Table from one location to another and retain all formulas, formats and columnwidths?

3) Line break in vba message box in Excel

4) How do I enter a formula in a cell using VBA (using Absolute Reference)?

5) How can I get users to select a folder to save the output of my macro?

6) How can I sort data using VBA?

7) How can I protect / unprotect WorkSheet using VBA?

8) Excel 2010 vba replacement of application filesearch in Excel

9) I have a macro that takes a lot of time for execution - how can I keep the user informed that the macro is running?

10) How can worksheet functions be accessed in VBA?

11) How can I use SUMPRODUCT to summarize my raw data?

12) How can I set up ListBox using VBA to allow users to select multiple values?

13) Vba list all files in a folder in Excel

14) Is there a way to easily shade alternate rows in a data range for improving readability?

15) I have a complex voluminous workbook with lot of formulas. Every time I make an update, the processing is slow. Any workarounds available?