我有一个代码,我首先选择一组单元格,然后创建一个图表。这些单元格是输入,因此只要有新条目,图表就会更新。 部分代码如下。我需要将源范围定义为我之前选择的单元格。知道如何做到这一点吗?
Range(Selection, Selection.End(xlUp)).Select
Range(Selection, Selection.End(xlUp)).Select
Range(Selection, Selection.Offset(0, 5)).Select
Range(Selection, Selection.Offset(1, 0)).Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Range("Blad3!$B$3:$G$75")
ActiveChart.SeriesCollection(2).Select
ActiveChart.SeriesCollection(2).ChartType = xlLine
答案 0 :(得分:0)
第一个解决方案:不使用ActiveChart,而是为新创建的图表使用变量:
Dim c As Chart
Range("whatever").Select
Set c = ActiveSheet.Shapes.AddChart.Chart
c.ChartType = xlLineMarkers
c.SetSourceData Source:=Selection
c.SeriesCollection(2).ChartType = xlLine
或者,在With
:
Range("whatever").Select
With ActiveSheet.Shapes.AddChart.Chart
.ChartType = xlLineMarkers
.SetSourceData Source:=Selection
.SeriesCollection(2).ChartType = xlLine
'...
End With
第二种解决方案:您可以将选择保存在变量中:
Dim r As Range
Set r = Range("whatever")
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=r
ActiveChart.SeriesCollection(2).ChartType = xlLine
当然,你可以使用这两种方法。
答案 1 :(得分:0)
Dim lastRow As Long
With Sheets("blad3")
lastRow = .Range("E" & Rows.Count).End(xlUp).Row
Application.ScreenUpdating = False
Sheets("Blad1").Select
Range("C11").Select
Range(Selection, Selection.End(xlDown)).Select
Selection.Copy
Sheets("Blad3").Select
Range("A3").Select
ActiveSheet.Paste
Range("e3").Select
ActiveSheet.Paste
Range("A3").Select
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLineMarkers
ActiveChart.SetSourceData Source:=Sheets("Blad3").Range("b3:E" & lastRow)
ActiveChart.SeriesCollection(1).Select
ActiveChart.SeriesCollection(1).ChartType = xlLine
ActiveChart.PlotArea.Select