0

I'm trying to select a graph on a worksheet which already exists and edit the data range by assigning it based on values in another sheet where it knows to keep looking for data until it runs out of columns.

I can't get it work with it not recognising the object correctly and other online help hasn't worked.

Sub GraphData()

Dim chrt As Chart

Sheets("ICB Time Series").ChartObjects("Chart1a").Select

Set chrt = ActiveChart

        With Sheets("Charts Data")
            chrt.SetSourceData Source:=.Range("B22:B23").End(xlToRight).Column
        End With

End Sub
2
  • 2
    Note that .Column returns the column index, not a range. Commented Feb 7, 2023 at 16:25
  • Ah thank you, how would I edit it to include the range from column B to the final populated column? Commented Feb 7, 2023 at 16:39

1 Answer 1

1

Try something like this:


Sub GraphData()

    Dim chrt As Chart, rng As Range

    'get a reference to the chart (no need to use ActiveChart)
    Set chrt = Sheets("ICB Time Series").ChartObjects("Chart1a").Chart

    With Sheets("Charts Data")
        Set rng = .Range("B22", .Cells(22, Columns.Count).End(xlToLeft).Offset(1).Address(False, False))
        Debug.Print "Setting range", rng.Address()
    End With
    chrt.SetSourceData Source:=rng

End Sub
Sign up to request clarification or add additional context in comments.

Comments

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.