0

I am trying to set the define a range variable and then use this range variable as a source for an Excel chart. But I am getting the Object Required Error when i run my Macro at Set rng =

 Sub temp3()
'
' temp3 Macro
'
    Dim rng As Range
    Range("A1").Select
    Range(Selection, Selection.End(xlDown)).Select
    Set rng = Range(Selection, Selection.End(xlToRight)).Select

    ActiveSheet.Shapes.AddChart2(332, xlLineMarkers).Select
    ActiveChart.SetSourceData Source:=rng
End Sub
1
  • 1
    Try it without the .Select method at the end, i.e. Set rng = Range(Selection,Selection.End(xlToRight)) Commented Jan 2, 2018 at 19:35

2 Answers 2

2

Do not use .Select

 Sub temp3()
'
' temp3 Macro
'
    Dim rng As Range
    Dim shp As Chart
    With Worksheets("Sheet1") 'Change to your sheet
        Set rng = .Range("A1", .Range("A1").End(xlDown).End(xlToRight))

        Set shp = Charts.Add
        Set shp = shp.Location(Where:=xlLocationAsObject, Name:=.Name)
        With shp
            .ChartType = xlLineMarkers
            .SetSourceData rng
        End With
    End With
End Sub
Sign up to request clarification or add additional context in comments.

Comments

0

You can also create named range and set the data source of chart to named range. you will then use vba to change the range of the named range. That method may be simpler. See How do I redefine a named range with VBA? on how to change named ranged by vba

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.