Results 1 to 3 of 3

Thread: Change Chart Data Source To Different Column Ranges Using VBA

  1. #1
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0

    Change Chart Data Source To Different Column Ranges Using VBA

    Hi

    I spent last day or so trying to create a macro that would update the data source of the series in the chart. I went thorugh dozens of solutions which I found on the net and none of them are doing what I need them to do.

    The problem is that the series of data on the chart pull data from different columns of the data source table.

    This is what I get when I update the Data Source and record the macro:

    Code:
    ActiveChart.SetSourceData Source:=Sheets(1).Range("L12:L65,N12:N65")
    what I would like to replace it with is the following:

    Code:
    Dim MyDataSource1 as Range
    Dim MyDataSource2 as Range
    
    
    LastCellColumnL = ....
    LastCellColumnN = ...
    Set MyDataSource1 = ActiveSheets.Range(L12, LastCellColumnL)
    Set MyDataSource2 = ActiveSheets.Range(N12, LastCellColumnN)
    
    ActiveChart.SetSourceData Source:=Sheets(1).Range(MyDataSource1,MyDataSource2)
    The problem is that why I do this, it does not treat it the same way as the original line, it also includes all the columns in between the two data ranges.
    How to make

    Code:
    ActiveChart.SetSourceData Source:=Sheets(1).Range(MyDataSource1,MyDataSource2)
    to work like:

    Code:
    ActiveChart.SetSourceData Source:=Sheets(1).Range("L12:L65,N12:N65")
    and only include the two columns like in the original line here?

    Rafal
    Last edited by Admin; 04-26-2013 at 04:42 PM. Reason: code tag added

  2. #2
    Administrator Admin's Avatar
    Join Date
    Mar 2011
    Posts
    1,123
    Rep Power
    10
    Hi Rafal,

    Welcome to ExcelFox!!

    PLease use code tags. This time I added for you.

    Try

    Code:
    ActiveChart.SetSourceData Source:=Union(MyDataSource1, MyDataSource2)
    Cheers !

    Excel Range to BBCode Table
    Use Social Networking Tools If You Like the Answers !

    Message to Cross Posters

    @ Home - Office 2010/2013/2016 on Win 10 (64 bit); @ Work - Office 2016 on Win 10 (64 bit)

  3. #3
    Junior Member
    Join Date
    Apr 2013
    Posts
    5
    Rep Power
    0
    Quote Originally Posted by Admin View Post
    Hi Rafal,

    Welcome to ExcelFox!!

    PLease use code tags. This time I added for you.

    Try

    Code:
    ActiveChart.SetSourceData Source:=Union(MyDataSource1, MyDataSource2)
    It works, thank you so much.

    Also, I have noticed that updating the data source changes axis lables to default values (1,2,3,4...)
    I had to implement additional piece of code:

    Code:
    DestBook.Worksheets(4).ChartObjects("Chart 61").Chart.SeriesCollection(1).XValues = MyDataSource3

Similar Threads

  1. Replies: 1
    Last Post: 05-03-2013, 04:41 PM
  2. Replies: 3
    Last Post: 03-05-2013, 03:57 PM
  3. VBA code to copy data from source workbook
    By Howardc in forum Excel Help
    Replies: 1
    Last Post: 07-30-2012, 09:28 AM
  4. Replies: 3
    Last Post: 05-14-2012, 11:30 AM
  5. Get Chart's Source Data VBA
    By Admin in forum Excel and VBA Tips and Tricks
    Replies: 2
    Last Post: 11-24-2011, 07:54 AM

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •