我正在编写一个VBA函数来自动从数据集创建图表。
Sub create_graph
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Values = "='Blad1'!$F$1:$F$13"
ActiveChart.SeriesCollection.NewSeries
ActiveChart.SeriesCollection(1).Name = "=""Kosten"""
End sub
这很好用。我多么想让它变得更有活力。所以我包括:
Range("B6").Select
Set x = Range(Selection, Selection.End(xlDown))
所以我的代码现在就是这样:
Sub create_graph
ActiveSheet.Shapes.AddChart.Select
ActiveChart.ChartType = xlLine
ActiveChart.SeriesCollection.NewSeries
'Nog even kijken hoe ik dit dynamisch kan maken
Range("B6").Select
Set x = Range(Selection, Selection.End(xlDown))
ActiveChart.SeriesCollection(1).Values = x
ActiveChart.SeriesCollection.NewSeries
End sub
但他的错误91。
关于出了什么问题的任何想法?
答案 0 :(得分:0)
请尝试以下代码。
将代码行中 Sheet_Name 的值修改为工作表名称Set ws = ActiveWorkbook.Worksheets("Sheet_Name")
Sub create_graph()
Dim ws As Excel.Worksheet
Dim x As Range
' modify Sheet_name to your worksheet name
Set ws = ActiveWorkbook.Worksheets("Sheet_Name")
' Range("B6").Select
Set x = ws.Range("B6:B" & ws.Cells(ws.Rows.count, "B").End(xlUp).Row)
ws.Shapes.AddChart.Select
With ActiveChart
.ChartType = xlLine
.SeriesCollection.NewSeries
.SeriesCollection(1).Values = x
End With
End Sub