Excel VBA 使用 .SetSourceData 设置图表的源数据

问题描述

我正在尝试更新工作表中图表的源数据范围,但出现运行时错误“445”。文档声称 Chartobject.Chart.SetSourceData Source:=Range 应该是有效的,但是当我尝试设置 FirstChart 的源数据时,为什么我的代码失败了?当我将 PlotBy 修饰符添加到 SecondChart 时,我也会收到一个不同的错误 (-2147467259 80004005)。

这是我的代码

Public Sub ReGenerateCharts()

Dim Ws As Worksheet
Set Ws = Worksheets("Chart Sheet")
Dim FirstBottomCell As Long

Dim SecondBottomCell As Long

Dim FirstSourceData As Range
Dim SecondSourceData As Range

Dim FirstChart As ChartObject
Dim SecondChart As ChartObject

With Ws
    Set FirstChart = .ChartObjects("Chart 1")
    Set SecondChart = .ChartObjects("Chart 2")
    
    FirstBottomCell = .Cells(.Rows.Count,15).End(xlUp).Row
    SecondBottomCell = .Cells(.Rows.Count,24).End(xlUp).Row
    
    Set FirstSourceData = .Range(.Cells(4,15),.Cells(FirstBottomCell,15))
    Set SecondSourceData = .Range(.Cells(4,24),.Cells(SecondBottomCell,24))

End With
FirstChart.Chart.ChartType = xlHistogram
SecondChart.Chart.ChartType = xlHistogram

FirstChart.Chart.SetSourceData Source:=FirstSourceData
SecondChart.Chart.SetSourceData Source:=SecondSourceData,PlotBy:=xlRows
End Sub

解决方法

暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!

如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。

小编邮箱:dio#foxmail.com (将#修改为@)