动态添加系列到图表
Dynamically add series to a chart
我正在尝试创建一个 excel,它使用几个数据验证切换列表和一个按钮,允许用户显示模型构建的特定阶段的特定图。为此,我需要能够向图表动态添加系列。我在网上找到了很多关于此的 material,但是尽管我尽了最大的努力,我还是无法让我的脚本工作。 if 条件中的行似乎总是生成以下错误“运行 时间错误‘1004’:应用程序定义或对象定义的错误”。
任何帮助将不胜感激。
Sub UpdateChart()
'declaring variables'
Dim chrt As ChartObject
Dim chrtsercoll As SeriesCollection
Dim chtser As Series
'create the series collection from the chart'
Set chrt = ActiveSheet.ChartObjects(1)
'Get the series collection from the chart'
Set chrtsercoll = chrt.Chart.SeriesCollection
'delete all existing series in chart'
For Each chtser In chrtsercoll
chtser.Delete
Next chtser
'set up series in case of residual plot'
If Range("C21").Value = "residual series" Then
With chrtsercoll.NewSeries()
.Name = "=" & ActiveSheet.Name & "!B15"
.Values = "=" & ActiveSheet.Name & "!" & Evaluate(ActiveSheet.Names("RSr").Value)
End With
ActiveSheet.ChartObjects(1).Chart.ChartType = xlLine
End If
End Sub
像这样的东西应该有用。
- 总是用作品限定范围引用等sheet
- 如果 sheet 名称有空格,请在范围引用中用引号将 sheet 名称括起来
- 您没有为系列分配任何内容
XValues
- 不清楚
Evaluate(ActiveSheet.Names("RSr").Value)
是否有任何问题 - 也许你可以解释一下你在那里做什么?
Sub UpdateChart()
Dim ws As Worksheet
Set ws = ActiveSheet
With ws.ChartObjects(1).Chart
'remove existing data
Do While .SeriesCollection.Count > 0
.SeriesCollection(1).Delete
Loop
'set up series in case of residual plot'
If ws.Range("C21").Value = "residual series" Then
With .SeriesCollection.NewSeries
.Name = "='" & ws.Name & "'!B15" 'use quotes in case name has spaces
.XValues = 1 'you need to add something here....
.Values = "='" & ws.Name & "'!" & Evaluate(ActiveSheet.Names("RSr").Value)
End With
.ChartType = xlLine
End If
End With
End Sub
刚刚找到了我的评论的答案,感谢通过实际关注我在我的 excel 中输入的内容,并且 VBA 告诉你当你将鼠标悬停在它上面时你的函数等于什么。只是想为了完整起见,我会把正常工作的代码放在这里。再次非常感谢蒂姆!
Sub UpdateChart()
Dim ws As Worksheet
Set ws = ActiveSheet
With ws.ChartObjects(1).Chart
'remove existing data'
Do While .SeriesCollection.Count > 0
.SeriesCollection(1).Delete
Loop
'set up series in case of residual plot'
If ws.Range("C21").Value = "residual series" Then
With .SeriesCollection.NewSeries
.Name = "='" & ws.Name & "'!B15" 'use quotes in case sheet name has spaces'
.XValues = "=" & ws.Range("AC22").Value
.Values = "=" & ws.Range("AC15").Value
End With
.ChartType = xlLine
End If
End With
End Sub
我正在尝试创建一个 excel,它使用几个数据验证切换列表和一个按钮,允许用户显示模型构建的特定阶段的特定图。为此,我需要能够向图表动态添加系列。我在网上找到了很多关于此的 material,但是尽管我尽了最大的努力,我还是无法让我的脚本工作。 if 条件中的行似乎总是生成以下错误“运行 时间错误‘1004’:应用程序定义或对象定义的错误”。 任何帮助将不胜感激。
Sub UpdateChart()
'declaring variables'
Dim chrt As ChartObject
Dim chrtsercoll As SeriesCollection
Dim chtser As Series
'create the series collection from the chart'
Set chrt = ActiveSheet.ChartObjects(1)
'Get the series collection from the chart'
Set chrtsercoll = chrt.Chart.SeriesCollection
'delete all existing series in chart'
For Each chtser In chrtsercoll
chtser.Delete
Next chtser
'set up series in case of residual plot'
If Range("C21").Value = "residual series" Then
With chrtsercoll.NewSeries()
.Name = "=" & ActiveSheet.Name & "!B15"
.Values = "=" & ActiveSheet.Name & "!" & Evaluate(ActiveSheet.Names("RSr").Value)
End With
ActiveSheet.ChartObjects(1).Chart.ChartType = xlLine
End If
End Sub
像这样的东西应该有用。
- 总是用作品限定范围引用等sheet
- 如果 sheet 名称有空格,请在范围引用中用引号将 sheet 名称括起来
- 您没有为系列分配任何内容
XValues
- 不清楚
Evaluate(ActiveSheet.Names("RSr").Value)
是否有任何问题 - 也许你可以解释一下你在那里做什么?
Sub UpdateChart()
Dim ws As Worksheet
Set ws = ActiveSheet
With ws.ChartObjects(1).Chart
'remove existing data
Do While .SeriesCollection.Count > 0
.SeriesCollection(1).Delete
Loop
'set up series in case of residual plot'
If ws.Range("C21").Value = "residual series" Then
With .SeriesCollection.NewSeries
.Name = "='" & ws.Name & "'!B15" 'use quotes in case name has spaces
.XValues = 1 'you need to add something here....
.Values = "='" & ws.Name & "'!" & Evaluate(ActiveSheet.Names("RSr").Value)
End With
.ChartType = xlLine
End If
End With
End Sub
刚刚找到了我的评论的答案,感谢通过实际关注我在我的 excel 中输入的内容,并且 VBA 告诉你当你将鼠标悬停在它上面时你的函数等于什么。只是想为了完整起见,我会把正常工作的代码放在这里。再次非常感谢蒂姆!
Sub UpdateChart()
Dim ws As Worksheet
Set ws = ActiveSheet
With ws.ChartObjects(1).Chart
'remove existing data'
Do While .SeriesCollection.Count > 0
.SeriesCollection(1).Delete
Loop
'set up series in case of residual plot'
If ws.Range("C21").Value = "residual series" Then
With .SeriesCollection.NewSeries
.Name = "='" & ws.Name & "'!B15" 'use quotes in case sheet name has spaces'
.XValues = "=" & ws.Range("AC22").Value
.Values = "=" & ws.Range("AC15").Value
End With
.ChartType = xlLine
End If
End With
End Sub