Chart - Series1 and series 2 lines how to name

Patriot2879

Well-known Member
Joined
Feb 1, 2018
Messages
1,081
Hi good morning all, hope you can help me please, i have the code below, which makes a line chart, and i have named my x and y axis, but on the right hand side it says which line is which and is colour coded, one line is called series 1 and the other is called series 2, but i cant see any where in my code where to rename these 2 lines to what i want to call them, please can you advise please?
VBA Code:
Private Sub CommandButton4_Click()
' declare variables for last row and worksheet
    Dim lastRow
    Dim sh As Worksheet
    Set sh = ActiveWorkbook.Worksheets("Sheet1")
' get last populated cell in column A
    lastRow = sh.Cells(Rows.Count, "A").End(xlUp).Row
    Application.ScreenUpdating = False
    On Error Resume Next
    ThisWorkbook.Charts.Delete
    On Error GoTo 0

    Dim ChartSheet1 As Chart
    Set ChartSheet1 = Charts.Add


    With ChartSheet1
        .SetSourceData Source:=Sheets("Sheet1").Range("C2:C" & lastRow & ",D2:D" & lastRow)
        .ChartType = xlLineMarkers
        ActiveChart.PlotArea.Format.Fill.ForeColor.RGB = RGB(208, 254, 202)
        'X axis name
.Axes(xlCategory, xlPrimary).HasTitle = True
.Axes(xlCategory, xlPrimary).AxisTitle.Characters.Text = "Miles"
'y-axis name
.Axes(xlValue, xlPrimary).HasTitle = True
.Axes(xlValue, xlPrimary).AxisTitle.Characters.Text = "Pace"
.HasTitle = True
.ChartTitle.Text = "Dist / Avg Pace"

        
    End With
    Application.ScreenUpdating = True
    Unload Me
End Sub
 

Some videos you may like

Excel Facts

What is the shortcut key for Format Selection?
Ctrl+1 (the number one) will open the Format dialog for whatever is selected.

Momentman

Well-known Member
Joined
Jan 11, 2012
Messages
4,064
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Within the With block, you can maybe do
VBA Code:
.SeriesCollection(1).Name = "First name"
.SeriesCollection(2).Name = "Second name"
 

Watch MrExcel Video

Forum statistics

Threads
1,114,525
Messages
5,548,558
Members
410,849
Latest member
DannyNg
Top