0

I am trying to achieve dynamically chart Time Series

To do this I am copying Dates and Profit from Data Orders from my raw data to another sheet where I will use it to dynamically create chart.

But it is giving me an error. near the line where I am creating range, is there a better way to do this ?

Sub ChartDataSeries()

    Dim ws As Worksheet
    Dim ws1 As Worksheet
    Dim rng As Range

    Set ws = Sheets("Data - Orders")
    Set ws1 = Sheets("Sheet10")
    ws.Range("C:C").Copy 'Source Copy Order Date
    ws1.Activate
    ws1.Range("B1").Select 'Paste Order Date
    ActiveSheet.Paste

    ws.Range("I:I").Copy 'Copy Profit Series
    ws1.Activate
    ws1.Range("C1").Select
    ActiveSheet.Paste ' Paste Profit Series
    ws1.Range("B1").Select
    Set rng = Range(Selection, Selection.End(xlToRight)).Select ' Error Ocurring here
    ActiveSheet.ChartObjects("Chart 7").Activate
    ActiveChart.SetSourceData Source: rng.Select
    'ActiveChart.SeriesCollection(1).XValues = Range("Order Date")
    'ActiveChart.SeriesCollection(1).Values = Range("Profit")



End Sub

1 Answer 1

2

It's better if you stay away from all this Activate, Select, Selection, ActiveChart, etc..

Replace the following 4 lines:

ws1.Range("B1").Select
Set rng = Range(Selection, Selection.End(xlToRight)).Select ' Error Ocurring here
ActiveSheet.ChartObjects("Chart 7").Activate
ActiveChart.SetSourceData Source: rng.Select

With these 2:

Set Rng = ws1.Range(ws1.Range("B1"), ws1.Range("B1").End(xlToRight))
ActiveSheet.ChartObjects("Chart 7").Chart.SetSourceData Source:=Rng
Sign up to request clarification or add additional context in comments.

3 Comments

@avabhishiek you can't Set a Range object, and Select it at the same line. You need 1 line to Set, and you realy don't need to Select it to use it as a Chart's SetSourceData
@avabhishiek please mark this as "ASNWER", you have many post lately you don't five credit to people who helped you
sorry about that , I have marked it as answer , thanks for the help

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.