Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Set Legend of a Chart from VBA?

I'm trying to set the Legend caption of a Chart by referencing a cell in a
different sheet but

ActiveChart.Legend.LegendEntries(1) =
ActiveWorkbook.Sheets("Sheet2").Range("F2").Value

gives me "Object doesn't Support this property or method".

Is there anyway to do this?

  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Set Legend of a Chart from VBA?

Try...

ActiveChart.SeriesCollection(1).Name = ActiveWorkbook.Sheets("Sheet2").Range("F2").Text

Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Mike B in VT"
wrote in message...
I'm trying to set the Legend caption of a Chart by referencing a cell in a
different sheet but

ActiveChart.Legend.LegendEntries(1) =
ActiveWorkbook.Sheets("Sheet2").Range("F2").Value

gives me "Object doesn't Support this property or method".
Is there anyway to do this?
  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Set Legend of a Chart from VBA?

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.

"Jim Cone" wrote:

Try...

ActiveChart.SeriesCollection(1).Name = ActiveWorkbook.Sheets("Sheet2").Range("F2").Text

Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Mike B in VT"
wrote in message...
I'm trying to set the Legend caption of a Chart by referencing a cell in a
different sheet but

ActiveChart.Legend.LegendEntries(1) =
ActiveWorkbook.Sheets("Sheet2").Range("F2").Value

gives me "Object doesn't Support this property or method".
Is there anyway to do this?

  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Set Legend of a Chart from VBA?

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.



"Jim Cone" wrote:
Try...
ActiveChart.SeriesCollection(1).Name = ActiveWorkbook.Sheets("Sheet2").Range("F2").Text
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Mike B in VT"
wrote in message...
I'm trying to set the Legend caption of a Chart by referencing a cell in a
different sheet but

ActiveChart.Legend.LegendEntries(1) =
ActiveWorkbook.Sheets("Sheet2").Range("F2").Value

gives me "Object doesn't Support this property or method".
Is there anyway to do this?

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 4
Default Set Legend of a Chart from VBA?

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.



"Jim Cone" wrote:
Try...
ActiveChart.SeriesCollection(1).Name = ActiveWorkbook.Sheets("Sheet2").Range("F2").Text
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Mike B in VT"
wrote in message...
I'm trying to set the Legend caption of a Chart by referencing a cell in a
different sheet but

ActiveChart.Legend.LegendEntries(1) =
ActiveWorkbook.Sheets("Sheet2").Range("F2").Value

gives me "Object doesn't Support this property or method".
Is there anyway to do this?




  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 3,290
Default Set Legend of a Chart from VBA?

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.



"Jim Cone" wrote:
Try...
ActiveChart.SeriesCollection(1).Name = ActiveWorkbook.Sheets("Sheet2").Range("F2").Text
Jim Cone
San Francisco, USA
http://www.realezsites.com/bus/primitivesoftware



"Mike B in VT"
wrote in message...
I'm trying to set the Legend caption of a Chart by referencing a cell in a
different sheet but

ActiveChart.Legend.LegendEntries(1) =
ActiveWorkbook.Sheets("Sheet2").Range("F2").Value

gives me "Object doesn't Support this property or method".
Is there anyway to do this?


Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
add legend to chart T.H Charts and Charting in Excel 1 July 30th 09 10:53 AM
Legend on Chart Double L Charts and Charting in Excel 7 June 11th 08 07:01 PM
Bar Chart Legend Key Certior Charts and Charting in Excel 3 September 26th 07 03:51 PM
Chart Legend PCLIVE Charts and Charting in Excel 6 May 5th 06 01:42 AM
pie chart legend CM Charts and Charting in Excel 0 October 17th 05 11:17 PM


All times are GMT +1. The time now is 03:11 AM.

Powered by vBulletin® Copyright ©2000 - 2025, Jelsoft Enterprises Ltd.
Copyright ©2004-2025 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"