Excel - ActiveChart.SetSourceData Source: Runtime Error '445' : Object doesn't support this action

不问归期 提交于 2021-01-29 19:01:31

问题


I have an excel workbook (v2016), where I managed to get the table name of a table based on the highest number of entries. This value (which is a name of a table) is in Sheet "Pareto" Cell B4.

I'm using the following code to update a chart using an above-mentioned table name

Sub GetTables()

Dim YTDL1TableName As String
Dim YTDL1Range As Range

YTDL1TableName = Sheets("Pareto").Range("B4")
Set YTDL1Range = Sheets("Pareto").Range(Sheets("Pareto").ListObjects(YTDL1TableName).Range.Address(True, True))
Sheets("Pareto").ChartObjects("YTDL1").Activate
ActiveChart.ChartArea.Select
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = YTDL1TableName
ActiveChart.SetSourceData Source:=YTDL1Range
End Sub

While the charts do get updated I do get an error

Runtime Error '445' : Object doesn't suuport this action

How do I get rid of this error?

Thanks in advance


回答1:


As I understand it, you want to change the data source of the chart based on the values in the cell.

Please try the code below:

Sub GetTables()

Dim YTDL1TableName As String
Dim YTDL1Range As Range


YTDL1TableName = Sheets("Pareto").Range("B4")
'Set YTDL1Range = Sheets("Pareto").Range(Sheets("Pareto").Cells(YTDL1TableName).Address(True, True))
Worksheets("Pareto").ChartObjects("YTDL1").Activate
ActiveChart.ChartArea.Select
ActiveChart.HasTitle = True
ActiveChart.ChartTitle.Text = YTDL1TableName
ActiveChart.SetSourceData Source:=Sheets("Pareto").Range("B4")

End Sub

Here is the result:

Hope that helps!



来源:https://stackoverflow.com/questions/52159008/excel-activechart-setsourcedata-source-runtime-error-445-object-doesnt-s

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!