+ Reply to Thread
Results 1 to 6 of 6

Chart source data

  1. #1
    Registered User
    Join Date
    10-04-2023
    Location
    Paris
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2309 Build 16.0.16827.20130) 32-bit
    Posts
    2

    Thumbs up Chart source data

    Hello,
    In my VBA code, I calculate a matrix M of dimension (x, 1) where x is a variable number. Now I want plot M in a x-y graph. How can I source the data from the matrix? Searching on internet I found the command SetSourceData, but it requires as input a range, like:
    Please Login or Register  to view this content.
    There is a way to source the data for the graph directly from M?
    Last edited by alansidman; 10-04-2023 at 11:58 AM.

  2. #2
    Forum Moderator jeffreybrown's Avatar
    Join Date
    02-19-2009
    Location
    Cibolo, TX
    MS-Off Ver
    Office 365
    Posts
    10,316

    Re: Chart source data

    Without seeing a sample of what you are trying, I find it difficult to help with a solution. Sometimes the easiest way to get what you are searching for in VBA is to manually record the macro while building the chart. Have you tried this?
    HTH
    Regards, Jeff

  3. #3
    Registered User
    Join Date
    10-04-2023
    Location
    Paris
    MS-Off Ver
    Microsoft? Excel? for Microsoft 365 MSO (Version 2309 Build 16.0.16827.20130) 32-bit
    Posts
    2

    Re: Chart source data

    Here an example:

    Please Login or Register  to view this content.
    Now I want to write a code for plotting in a x-y graph x=M(i, 0) versus y=M(i, 1), i.e. y=x^2.
    The data are stored in matrix M, not in any spreadsheet cells. I found a lot of tutorials on internet about creating graph in VBA. All of them assume to source the graph data from spreadsheet cells. I am looking for a method for sourcing the data directly from M.

  4. #4
    Forum Expert rorya's Avatar
    Join Date
    08-13-2008
    Location
    East Sussex, UK
    MS-Off Ver
    365 Ent Monthly Channel / Insiders Beta
    Posts
    8,913

    Re: Chart source data

    SetSourceData does only work with ranges. You need to assign the array to the Values property of the relevant series, but be aware that there is a limit to the length of the formula you can assign to a series and your values will be part of the literal formula. It's generally safer to just assign the array to a range, and then use that for the chart.
    Rory

  5. #5
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Chart source data

    A graph can only source data from worksheet cells. Chart SERIES formulas require cell ranges (or named ranges) as arguments. There is no other way to build a chart. You will have to write your matrix to a worksheet then use the first line of code you showed to build the graph from that range.

    BTW you said x is a variable number but your example shows 9 hard-coded, so my example is also hard-coded.

    Please Login or Register  to view this content.
    where Sheet Name is the name of a worksheet that you arbitrarily pick to store the data. It can be hidden.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  6. #6
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: Chart source data

    rorya has a possibility that I overlooked which is putting the array of values directly into the SERIES formula. However, IMHO it is better to put the data in a worksheet.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Similar Threads

  1. [SOLVED] Create or add chart data source buttons outside of chart
    By aku8 in forum Excel Charting & Pivots
    Replies: 9
    Last Post: 09-02-2021, 06:12 PM
  2. Excel chart with multi level category label issue delinking source data from chart
    By babu324 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 12-19-2017, 02:58 AM
  3. Replies: 3
    Last Post: 05-05-2015, 12:43 PM
  4. To get Chart Data Source of powerpoint chart by excel VBA
    By imran0305 in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 01-13-2014, 07:13 AM
  5. Chart not updated until chart sheet opened following source data update
    By untilyesterday in forum Excel Charting & Pivots
    Replies: 2
    Last Post: 09-24-2013, 12:27 PM
  6. How do you have one source data in a chart flow into a 2nd source of data?
    By Disintegrate in forum Excel Charting & Pivots
    Replies: 4
    Last Post: 10-06-2009, 09:26 AM
  7. Chart data source
    By Rob in forum Excel Charting & Pivots
    Replies: 6
    Last Post: 01-30-2005, 02:06 PM

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1