Home » excel » vba – Excel Variable Ranges while creating a chart

vba – Excel Variable Ranges while creating a chart

Posted by: admin April 23, 2020 Leave a comment

Questions:

So basically I need to be able to select the last row to create a chart using this method.

Sub createchart2()
    lastA = Range("A1").End(xlDown).Row
    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine
    ActiveChart.SetSourceData Source:=Range("Main!$A$3:$A$10")
End Sub

I need the range for A10 to be able to select the last row in the A column.

How to&Answers:

Is this what you are trying?

Sub createchart2()
    Dim lastA As Long

    lastA = Range("A" & Rows.Count).End(xlUp).Row

    ActiveSheet.Shapes.AddChart.Select
    ActiveChart.ChartType = xlLine

    ActiveChart.SetSourceData Source:=Range("Main!$A$3:$A$" & lastA)
End Sub