Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default help on charts

Dear experts,
I would like to insert a chart in a spreadsheet
automatically with VBA. The chart refers to 4 ranges in
rows, each range is a different data serie.
Sometimes it might be that a whole serie has no data in
it - so I get an error from VBA. I have bypassed it by
stating "On error resume next", but it has the
disadvantage that instead of getting in the legend the
name of serie where there are no data, I only get "serie
3" or "serie 4"... quite awful in a chart!
Is there a way I can get the name of the serie even if it
contains no data?
Many thanks in advance!
Best regards,
Valeria

In case it might help, here is my code:

ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range
("A20"), PlotBy:= _
xlRows
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).XValues = X_Data
ActiveChart.SeriesCollection(1).Values = Tot_Cost
ActiveChart.SeriesCollection(1).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 1, 4)
ActiveChart.SeriesCollection(2).Values = Int_Cost
ActiveChart.SeriesCollection(2).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 2, 4)
ActiveChart.SeriesCollection(3).Values = Ext_Cost
On Error Resume Next
ActiveChart.SeriesCollection(3).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 3, 4)
ActiveChart.SeriesCollection(4).Values = Price_Data
On Error Resume Next
ActiveChart.SeriesCollection(4).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 7, 4)
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 24
Default help on charts

You have entered the On Error Resume Next line after the
lines where you are setting the values. If the error is
occurring in the line where the values are being set then
the On Error Resume Next should be before those lines.

Try this:

On Error Resume Next
ActiveChart.SeriesCollection(3).Values = Ext_Cost
ActiveChart.SeriesCollection(3).Name = Workbooks _
(Product).Worksheets(1).Cells(ProductLastRow + 3, 4)
On Error Resume Next
ActiveChart.SeriesCollection(4).Values = Price_Data
ActiveChart.SeriesCollection(4).Name = Workbooks _
(Product).Worksheets(1).Cells(ProductLastRow + 7, 4)

When I did a little testing on this using a chart and data
from a macro of my own I did not get an error when there
was no data in a row that I had assigned as chart values.
But all my data was on the same sheet. This makes me think
that possibly you are pulling your data from different
sheets and the error occurs when the sheet has not been
automatically created or something. This would pose a
problem for trying to get a name - if the sheet that is
supposed to hold it is not there.

Assuming that missing sheets(or something like that) is
what you are facing. Just have your macro make a "scratch"
sheet that contains the titles for all the series and put
an If statement after each line that assigns the names
like this:

On Error Resume Next
ActiveChart.SeriesCollection(3).Values = Ext_Cost
ActiveChart.SeriesCollection(3).Name = Workbooks _
(Product).Worksheets(1).Cells(ProductLastRow + 3, 4)
If ActiveChart.SeriesCollection(3).Name = "Series 3" Then
ActiveChart.SeriesCollection(3).Name = Worksheets _
(ScratchSheet).Range(SeriesThreeName)
End If

'Add this somewhere after all the names have been assigned
Application.DisplayAlerts = False
Sheets(ScratchSheet).Delete
Application.DisplayAlerts = True


1) set "ScratchSheet" to the name of the page where you
put the names
2) set "SeriesThreeName" to the cell range on the scratch
sheet that has the name for series three

Have fun!

-IA


-----Original Message-----
Dear experts,
I would like to insert a chart in a spreadsheet
automatically with VBA. The chart refers to 4 ranges in
rows, each range is a different data serie.
Sometimes it might be that a whole serie has no data in
it - so I get an error from VBA. I have bypassed it by
stating "On error resume next", but it has the
disadvantage that instead of getting in the legend the
name of serie where there are no data, I only get "serie
3" or "serie 4"... quite awful in a chart!
Is there a way I can get the name of the serie even if it
contains no data?
Many thanks in advance!
Best regards,
Valeria

In case it might help, here is my code:

ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range
("A20"), PlotBy:= _
xlRows
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).XValues = X_Data
ActiveChart.SeriesCollection(1).Values = Tot_Cost
ActiveChart.SeriesCollection(1).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 1, 4)
ActiveChart.SeriesCollection(2).Values = Int_Cost
ActiveChart.SeriesCollection(2).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 2, 4)
ActiveChart.SeriesCollection(3).Values = Ext_Cost
On Error Resume Next
ActiveChart.SeriesCollection(3).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 3, 4)
ActiveChart.SeriesCollection(4).Values = Price_Data
On Error Resume Next
ActiveChart.SeriesCollection(4).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 7, 4)
.

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 1,071
Default help on charts

You could do something like the untested
If IsEmpty (Workbooks(Product).Worksheets(1) _
.Cells(ProductLastRow + 3, 4)) Then
ActiveChart.SeriesCollection(3).Name = "No data" _
Else
ActiveChart.SeriesCollection(3).Name = _
Workbooks(Product).Worksheets(1) _
.Cells(ProductLastRow + 3, 4)
End If
--
Regards,

Tushar Mehta, MS MVP -- Excel
www.tushar-mehta.com
Excel, PowerPoint, and VBA add-ins, tutorials
Custom MS Office productivity solutions

In article ,
says...
Dear experts,
I would like to insert a chart in a spreadsheet
automatically with VBA. The chart refers to 4 ranges in
rows, each range is a different data serie.
Sometimes it might be that a whole serie has no data in
it - so I get an error from VBA. I have bypassed it by
stating "On error resume next", but it has the
disadvantage that instead of getting in the legend the
name of serie where there are no data, I only get "serie
3" or "serie 4"... quite awful in a chart!
Is there a way I can get the name of the serie even if it
contains no data?
Many thanks in advance!
Best regards,
Valeria

In case it might help, here is my code:

ActiveChart.SetSourceData Source:=Sheets("Sheet1").Range
("A20"), PlotBy:= _
xlRows
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).XValues = X_Data
ActiveChart.SeriesCollection(1).Values = Tot_Cost
ActiveChart.SeriesCollection(1).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 1, 4)
ActiveChart.SeriesCollection(2).Values = Int_Cost
ActiveChart.SeriesCollection(2).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 2, 4)
ActiveChart.SeriesCollection(3).Values = Ext_Cost
On Error Resume Next
ActiveChart.SeriesCollection(3).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 3, 4)
ActiveChart.SeriesCollection(4).Values = Price_Data
On Error Resume Next
ActiveChart.SeriesCollection(4).Name = Workbooks
(Product).Worksheets(1).Cells(ProductLastRow + 7, 4)

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
charts toolbar / charts disappeared Pro Charts and Charting in Excel 0 December 18th 09 01:31 AM
link excel charts to web pages and update charts automatically Signguy Charts and Charting in Excel 1 April 22nd 08 08:29 PM
Charts - How to have multiple charts share a legend. Sean Charts and Charting in Excel 2 November 20th 07 04:49 AM
interactive charts for stacked bar charts [email protected] Charts and Charting in Excel 4 December 28th 06 09:58 PM
Matching the colors Column Charts and Pie Charts RohanSewgobind Charts and Charting in Excel 3 April 21st 06 09:35 PM


All times are GMT +1. The time now is 05:55 AM.

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

About Us

"It's about Microsoft Excel"