Excel Variable Ranges while creating a chart

2019-04-11 17:24发布

问题:

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.

回答1:

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