+ Reply to Thread
Results 1 to 15 of 15

Thread: Charting from internal data in a procedure

  1. #1
    Registered User
    Join Date
    11-26-2009
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2003
    Posts
    45

    Charting from internal data in a procedure

    Hello.

    I've made a procedure that performs calculations on a beam.

    I've divided the beam into x datapoints (each of which describes the forces in action at that particular point).

    The users aren't interested in seeing these data but only the resulting graphs.

    So basically i want to make charts from these datapoints (which i've stored internally in the procedure in different arrays) without having to plot the datapoints in the worksheet.

    Can anyone help please??

  2. #2
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Charting from internal data in a procedure

    This array can be assigned directly to a chart series.

    e.g.
    Sub AssignArray()
    Dim Points, Forces
      Points = Array(5, 15, 8)
      Forces = Array(1, 3, 9)
      
      ActiveChart.SeriesCollection(1).XValues = Points
      ActiveChart.SeriesCollection(1).Values = Forces
    End Sub
    Last edited by rwgrietveld; 11-26-2009 at 06:46 AM.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  3. #3
    Registered User
    Join Date
    11-26-2009
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Charting from internal data in a procedure

    Quote Originally Posted by rwgrietveld View Post
    This array can be assigned directly to a chart series.

    e.g.
    Sub AssignArray()
    Dim Points, Forces
      Points = Array(5, 15, 8)
      Forces = Array(1, 3, 9)
      
      ActiveChart.SeriesCollection(1).XValues = Points
      ActiveChart.SeriesCollection(1).Values = Forces
    End Sub
    Thx a lot for the quick response.


    EDIT: problem not solved yet...see next post
    Last edited by Kim1974; 11-26-2009 at 02:46 PM.

  4. #4
    Registered User
    Join Date
    11-26-2009
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Charting from internal data in a procedure

    I'm not quite there yet unfortunately:

    I've made an array containing the numbers of all the datapoints (in this case 1-100) which i want plottet on the x-axis and i want to plot some corresponding values from my beamcalculation on the y-axis.

    Here is some of the code i've written:

    Dim xArray(ANTALDATAPUNKTER) As Byte (the number of datapoints)
    Dim ufinTotalArray(ANTALDATAPUNKTER) As Single (an array containing the corresponding values to each x)
    .
    .
    . (a lot of calculations filling the ufinTotalarray with data and the xArray with the numbers 1-100 in this case)
    .
    .
    .
    And now i want to make the graf using the data from the arrays:

    Charts.Add
    ActiveChart.ChartType = xlColumnClustered
    ActiveChart.SeriesCollection(1).XValues = xArray
    ActiveChart.SeriesCollection(1).Values = ufinTotalArray
    ActiveChart.SeriesCollection(1).Name = "Deformation"


    But this doesn't work.

    Can you tell what's wrong??

    And while i'm at it, i've noticed that when charting using Excel (and not vba) each graf is given a shapename (like "shape xx") and when i copy the sheet this shapenumber changes. How can name the graph/shape manually?
    Last edited by Kim1974; 11-26-2009 at 12:40 PM.

  5. #5
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Charting from internal data in a procedure

    Almost there. need to add NewSeries ! and I belive you need a different chart type
    Sub datap()
    
    Const ANTALDATAPUNKTER = 100
    
    Dim xArray(1 To ANTALDATAPUNKTER) As Single 'Byte 'the number of datapoints
    Dim ufinTotalArray(1 To ANTALDATAPUNKTER) As Single 'an array containing the corresponding values to each x
    Dim x As Long
    
    For x = 1 To 100
      xArray(x) = x
      ufinTotalArray(x) = (x ^ 2) / 8 + 2 * x - 5
    Next x
    
    Charts.Add
    ActiveChart.ChartType = xlXYScatterSmooth
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).XValues = xArray
    ActiveChart.SeriesCollection(1).Values = ufinTotalArray
    ActiveChart.SeriesCollection(1).Name = "Deformation"
    
    End Sub
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  6. #6
    Forum Guru
    Join Date
    10-28-2008
    Location
    Not here anymore
    MS-Off Ver
    irrelevant
    Posts
    10,150

    Re: Charting from internal data in a procedure

    Hi Kim

    please put your code between code tags like this: [code] your code [/code]. That will make the code easier to read and copy. HTML otherwise strips out indentation and consecutive spaces.

  7. #7
    Registered User
    Join Date
    11-26-2009
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Charting from internal data in a procedure

    hello rwgrietveld

    I've tryed to run your procedure but i get a runtime error 1004 in this line:

    ActiveChart.SeriesCollection(1).XValues = xArray
    The error (translated from danish) reads something like this: "Can't define property XValues for the class Series"

  8. #8
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Charting from internal data in a procedure

    Each series formula has a maximum length of 1024 characters.

    So for a series with labels of a,b,c,d and data of 1,2,3,4 the series formula would look like this

    =SERIES(,{"a","b","c","d"},{1,2,3,4},1)

    If you divide the values by 3 then the formula balloons.
    =SERIES(,{"a","b","c","d"},{0.333333333333333,0.666666666666667,1,1.33333333333333},1)

    So if you have +100 decimal data values you have problably exceeded the limit.

    Jon Peliter has some code for delinking charts from ranges to array values.
    http://peltiertech.com/Excel/ChartsH...ChartData.html

    In the code is examples of dealing with this issue.
    Cheers
    Andy
    www.andypope.info

  9. #9
    Forum Guru rwgrietveld's Avatar
    Join Date
    09-02-2008
    Location
    Netherlands
    MS-Off Ver
    XL 2007 / XL 2010
    Posts
    1,671

    Re: Charting from internal data in a procedure

    Good addition Andy,

    Didn't know about the limit.

    Kim You could try to limit the number of points OR
    limit the accuracy. In a graph, this is most probably not significant.
    Looking for great solutions but hate waiting?
    Seach this Forum through Google

    www.Google.com
    (e.g. +multiple +IF site:excelforum.com/excel-general/ )

    www.Google.com
    (e.g. +fill +combobox site:excelforum.com/excel-programming/ )

    Ave,
    Ricardo

  10. #10
    Registered User
    Join Date
    11-26-2009
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Charting from internal data in a procedure

    Quote Originally Posted by rwgrietveld View Post
    Good addition Andy,

    Didn't know about the limit.

    Kim You could try to limit the number of points OR
    limit the accuracy. In a graph, this is most probably not significant.
    Thank you for the reply both of you (rwgrietveld and Andy Pope).

    rwgrietveld: you're right.

    Each corresponding calculation doesn't need to be that accurate since it is for chart-use only so i think i'll declare x as byte (since i never need more than 256 datapoints) and the result as integer (thus avoiding decimals).
    Last edited by Kim1974; 11-27-2009 at 06:44 AM.

  11. #11
    Registered User
    Join Date
    11-26-2009
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Charting from internal data in a procedure

    I just made a simple test:


    Sub datap()
    
    Const ANTALDATAPUNKTER = 82
    
    Dim xArray(1 To ANTALDATAPUNKTER) As Byte
    Dim ufinTotalArray(1 To ANTALDATAPUNKTER) As Integer
    Dim x As Byte
    
    For x = 1 To ANTALDATAPUNKTER
      xArray(x) = x
      ufinTotalArray(x) = x
    Next x
    
    Charts.Add
    ActiveChart.ChartType = xlXYScatterSmooth
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(1).XValues = xArray
    ActiveChart.SeriesCollection(1).Values = ufinTotalArray
    ActiveChart.SeriesCollection(1).Name = "Deformation"
    ActiveChart.Location Where:=xlLocationAsObject, Name:="Ark1"
    
    End Sub
    In this example (with 82 datapoints) i get the same runtime error whereas if i choose 81 datapoints (or less) it works fine.

    Since the value = the datapoint (ufinTotalArray(x) = x) it seems to me, that
    the SeriesCollection(1).Values only contains 82*(1+1) = 164 caracters (much less than 1024) and still it crashes??

  12. #12
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Charting from internal data in a procedure

    Frustrating is it. You have reached a limit in what the chart will allow with code and arrays.

    If you had a chart with a series that reference the values 1 to 81 in the range A1:A81 you could use this code to convert range to array.

    activechart.SeriesCollection(1).values=activechart.SeriesCollection(1).values
    Increase that to row 82 and the above will fail.

    Why do you not want to use a worksheet to hold the values?
    Cheers
    Andy
    www.andypope.info

  13. #13
    Registered User
    Join Date
    11-26-2009
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Charting from internal data in a procedure

    Why do you not want to use a worksheet to hold the values?
    Hello Andy.

    The reason i don't want the worksheet to hold the values is this:

    I'm making a worksheet that calculates different forces and deflections at x number of places in a beam (i "slice" the beam into x slices).

    Each sheet holds the information for one beam and i want each of these sheets to be cleansed of any unnecessary information. I only want the graphs depicting these forces and deflections and then some textoutput with maximum values.

    And then i want to be able to make multiple copies of the sheet so i can have multiple different beams (different after i change the input to the procedure that is) in one workbook.

    If i have one sheet for the beam and one sheet to hold the data for the beam there is to big a risc of confusion at some point

    EDIT: i forgot to mention, that i want to be able to manually delete some rows in the beam-sheet. If i put the data in that sheet and hides them by setting the colomnwith to 0, these data will be deleted
    Last edited by Kim1974; 11-27-2009 at 11:13 AM.

  14. #14
    Forum Guru Andy Pope's Avatar
    Join Date
    05-10-2004
    Location
    Essex, UK
    MS-Off Ver
    2003 & 2007 & 2010
    Posts
    11,349

    Re: Charting from internal data in a procedure

    If the chart is on a chart sheet then I can how a chart sheet AND data sheet may make things difficult to keep together.

    But you code moves the chart to a worksheet, which would suggest the chart and data could reside on the same worksheet.

    I'm still not getting why you can not use a hidden range to hold the chart information.
    Cheers
    Andy
    www.andypope.info

  15. #15
    Registered User
    Join Date
    11-26-2009
    Location
    Aarhus, Denmark
    MS-Off Ver
    Excel 2003
    Posts
    45

    Re: Charting from internal data in a procedure

    I'm still not getting why you can not use a hidden range to hold the chart information.
    Maybe you're right that i should just transfer the data from the arrays to the same worksheet as the beam-sheet and then make sure to refresh the data if i delete some of the rows

    Anyhow, thank you for taking the time to help me out.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

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.2.0