Loop that add named range as seriescollection to my chart VBA

espenskeie

Well-known Member
Joined
Mar 30, 2009
Messages
636
Office Version
  1. 2016
Platform
  1. Windows
Hi

I have a loop that I want to run to add different series to my chart... But I cannot find the right solution... Here is what I have come up with so far:

Code:
Sub NameMan()


    Dim wb As Workbook
    Dim ws As Worksheet, wsY As Worksheet
    Dim lr As Long, sht As Long
    Dim getdate As Date, todate As Date
    Dim shtName As String
    Dim Chart2 As ChartObject
    Dim wsX As Worksheet
    
       Set wb = ThisWorkbook
       Set ws = wb.Sheets("Graphics")
      
    For Each wsX In ThisWorkbook.Worksheets
    If wsX.Name <> "Graphics" Then
    k = k + 1
    
    shtName = wsX.Name
    
    Sheets(shtName).Activate
    
        With ActiveSheet
        lr = .Range("A2").End(xlDown).Row
        
        getdate = Sheets("Graphics").Range("P3").Value
        Res1 = Application.Match(CLng(getdate), .Range("A2:A" & lr), 0)
        
        todate = Sheets("Graphics").Range("P4").Value
        Res2 = Application.Match(CLng(todate), .Range("A2:A" & lr), 0)


        ws.Names.Add Name:="" & shtName & "", RefersToR1C1:="='" & shtName & "'!R" & Res1 + 1 & "C2:R" & Res2 + 1 & "C2"
        ws.Names("" & shtName & "").Comment = ""
        End With
        
    ws.ChartObjects("Chart 2").Activate
    ActiveChart.SeriesCollection(k).XValues = "='" & shtName & "'!A" & Res1 + 1 & ":A" & Res2 + 1 & ""
    ActiveChart.SeriesCollection(k).Values = ws.Range(shtName)


    End If
    Next
    ws.Activate
End Sub

It stops at this line:
Code:
ActiveChart.SeriesCollection(k).XValues = "='" & shtName & "'!A" & Res1 + 1 & ":A" & Res2 + 1 & ""

Kind regards
Espen
 

Excel Facts

Enter current date or time
Ctrl+: enters current time. Ctrl+; enters current date. Use Ctrl+: Ctrl+; Enter for current date & time.
Ok, all of a sudden the solution showed up:

Code:
ActiveChart.SeriesCollection.NewSeries
I was missing that line in here:

Code:
ws.ChartObjects("Chart 2").Activate
    ActiveChart.SeriesCollection.NewSeries
    ActiveChart.SeriesCollection(k).XValues = "='" & shtName & "'!A" & Res1 + 1 & ":A" & Res2 + 1 & ""
    ActiveChart.SeriesCollection(k).Values = Range(shtName)
 
Upvote 0

Forum statistics

Threads
1,203,046
Messages
6,053,193
Members
444,645
Latest member
mee siam

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