问题
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