Vba set source data in Excel

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

Description:

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 copy a Table from one location to another and retain all formulas, formats and columnwidths?

2) How can I update a listbox based on data in a list using VBA?

3) How can I find the number of working days between 2 dates using VBA?

4) How to print a worksheet in Excel VBA

5) How can I export a chart as a gif file?

6) How can I convert Column numbers into Column names for use in range definition?

7) How to add a link in a sheet to another sheet

8) Want to use Microsoft Outlook in Excel. Here some basic explanation to get you started

9) Line break in vba message box in Excel

10) How can I export a WorkSheet as a PDF using VBA?

 

Here the previous and next chapter