View Single Post
  #1   Report Post  
Posted to microsoft.public.excel.programming
Jim Cone Jim Cone is offline
external usenet poster
 
Posts: 3,290
Default Set Legend of a Chart from VBA?

More...
Have you verified the number of data points in a column don't exceed
the specification limits?
Jim Cone



"Jim Cone" wrote in message ...
Mike,
Do you have the correct sheet name?
Are there dots in front of "Range", "Cells" and "Rows" ?
Is the chart active? (selected)
All code goes in a sub or a function, with exceptions for
module level declarations such as Option Explicit etc.
A separate sub or function is not required for my posted code.
Jim Cone



"Mike B in VT"
wrote in message...
Hi Jim,
When I try using that code, I get an error when trying to do the assign to
..Values:
Error 1004 "Application-defined or Object Defined Error"
Do I have to do it in a subfunction like you wrote?

Thanks for all the help,
Mike



"Jim Cone" wrote:
Sounds like you could be exceeding the number of data points allowed.
Excel specifications (charts)...
Data series in one chart 255
Data points in a data series for 2-D charts 32,000
Data points in a data series for 3-D charts 4,000
Data points for all data series in one chart 256,000

The following code uses the data in column 1 for the series one values
and column 2 for series two and so on.
The "dots" are required.
--
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware

'------------------
Sub ChartSeriesTest()
Dim rngOne As Excel.Range
Dim rngTwo As Excel.Range
Dim rngThree As Excel.Range

With ActiveWorkbook.Sheets("Sheet2")
Set rngOne = .Range("A1", .Cells(.Rows.Count, 1).End(xlUp))
Set rngTwo = .Range("B1", .Cells(.Rows.Count, 2).End(xlUp))
Set rngThree = .Range("C1", .Cells(.Rows.Count, 3).End(xlUp))
End With

ActiveChart.SeriesCollection(1).Values = rngOne
ActiveChart.SeriesCollection(2).Values = rngTwo
ActiveChart.SeriesCollection(3).Values = rngThree

Set rngOne = Nothing
Set rngTwo = Nothing
Set rngThree = Nothing
End Sub
'---------------





"Mike B in VT"
wrote in message...
Great that worked, Thanks Jim.
As a follow on:
I'm trying to dynamically determine the series info for the chart.
Basically an Access query is going to populate Sheet2, then when the User
opens the spreadsheet I've got code in WorkbookOpen() to refresh the chart.
But the # number of values to be charted will change each export. Rather
than hard coding in an arbitrary high range like (a2:a10000), I only want to
use cells that have data in them.

I've tried ActiveChart.SeriesCollection(1).Values = (Sheets(2).UsedRange)
but get a type mismatch.