VBA Chart Series title with multiple cells

mikejvir

Board Regular
Joined
Jan 3, 2008
Messages
95
Hello All,

I am trying to make a more generic way to create a XY scatter chart and add data to the chart. Here is some test code.

VBA Code:
Sub Test_Add_Series()

Dim DataTab As String
Dim PLTab As String
Dim Xrange As Range
Dim Yrange As Range
Dim Trange As Range
Dim Snu As Long

DataTab = "Optics_Measurement_OPM_Append_0"
PLTab = "PL_Power"

Set Xrange = Worksheets(DataTab).Range(Cells(5, 8), Cells(7005, 8))
Set Yrange = Worksheets(DataTab).Range(Cells(5, 9), Cells(7005, 9))
Set Trange = Worksheets(DataTab).Range(Cells(3, 9), Cells(4, 9))
Snu = 1

Call CreateChartPage(PLTab)
Call Add_Series(DataTab, PLTab, Xrange, Yrange, Trange, Snu)

End Sub

Sub CreateChartPage(ChartDataTab As String)

  Range("EE1:EF5").Select
  Charts.Add.Name = ChartDataTab
  Charts(ChartDataTab).ChartType = xlXYScatterLinesNoMarkers
  Charts(ChartDataTab).Location Where:=xlLocationAsNewSheet
  Charts(ChartDataTab).SeriesCollection(1).Delete

End Sub

Sub Add_Series(DataTab As String, PLTab As String, Xrange As Range, Yrange As Range, Trange As Range, SPO As Long)
'

'  Generic Subroutine for creating a XY Scatter Graph in a seperate Chart Tab

' DataTab   : String   - Tab data is stored in
' PLtab     : String   - Chart will be put in this Tab
' Xrange    : Range    - Range for X axis Data
' Yrange    : Range    - Range for Y axis Data
' Trange    : Range    - Range for Series title
' SPO       : Long     - Series Plot order
'
    Application.ScreenUpdating = False
  '  Sheets(PLTab).Select
    Charts(PLTab).Activate
        
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(SPO).XValues = Xrange
    ActiveChart.SeriesCollection(SPO).Values = Yrange
    ActiveChart.SeriesCollection(SPO).Name = Worksheets(DataTab).Range("I3:I4") ' Trange
End Sub

I am stuck on the label for the series. The name is in multiple rows and when manually created this is fine. I tried both passing a range in and defined the range. Everything else is working as expected. I am missing something but can not see it.

Thanks in Advance.
 

Excel Facts

Difference between two dates
Secret function! Use =DATEDIF(A2,B2,"Y")&" years"&=DATEDIF(A2,B2,"YM")&" months"&=DATEDIF(A2,B2,"MD")&" days"
Hi mikejvr. It seems like this will work. The cells have to be added individually. The CStr is just to ensure that the cell values are used as a string as the name will error unless it receives a string input. Also, your code doesn't reset the screenupdating before you exit the sub. HTH. Dave
VBA Code:
ActiveChart.SeriesCollection(SPO).Name = CStr(Worksheets(DataTab).Range("I3")) & CStr(Worksheets(DataTab).Range("I4"))
Application.ScreenUpdating = True
 
Upvote 0
Hi mikejvr. It seems like this will work. The cells have to be added individually. The CStr is just to ensure that the cell values are used as a string as the name will error unless it receives a string input. Also, your code doesn't reset the screenupdating before you exit the sub. HTH. Dave
VBA Code:
ActiveChart.SeriesCollection(SPO).Name = CStr(Worksheets(DataTab).Range("I3")) & CStr(Worksheets(DataTab).Range("I4"))
Application.ScreenUpdating = True
Dave,

Thanks. That is a work around, but it forces the series label to be a string.

=SERIES("OPMPower",Optics_Measurement_OPM_Append_0!$H$5:$H$7005,Optics_Measurement_OPM_Append_0!$I$5:$I$7005,1)

but this is what I really am looking for

=SERIES(Optics_Measurement_OPM_Append_0!$I$3:$I$4,Optics_Measurement_OPM_Append_0!$H$5:$H$7005,Optics_Measurement_OPM_Append_0!$I$5:$I$7005,2)

I do this because I use Pivot Tables for plotting and the series label will change depending on the Pivot table values.

Mike Virostko
 
Upvote 0
Try using:

VBA Code:
    ActiveChart.SeriesCollection(SPO).Name = "='" & DataTab & "'!" & Trange.Address
 
Upvote 0
Solution

Forum statistics

Threads
1,224,463
Messages
6,178,817
Members
452,881
Latest member
motivationgyan

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top