由于 1004 运行 时间错误,无法使用录制的宏更改图表引用
Unable to change chart references with recorded macro due to 1004 run-time error
我正在尝试创建一个宏来重命名选项卡,然后将每个图表的源数据更改为命名范围。我通过 运行ning 通过每个图表记录了一个宏并手动更改源数据并获得了以下代码(通过删除 Application.CutCopyMode = False
和类似代码进行清理):
Sub UpdateRanges()
' UpdateRanges Macro
' Keyboard Shortcut: Ctrl+Shift+J
ActiveSheet.Name = "New"
ActiveSheet.ChartObjects("ProfSKU").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!EBITDA_Margin"
ActiveChart.FullSeriesCollection(2).Values = "==New!Gross_Margin"
ActiveSheet.ChartObjects("Pareto").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Pareto_Revenue"
ActiveChart.FullSeriesCollection(2).Values = "==New!Pareto_EBITDA"
ActiveChart.FullSeriesCollection(3).Values = "==New!Pareto_Volume"
ActiveChart.FullSeriesCollection(4).Values = "==New!Pareto"
ActiveSheet.ChartObjects("UnitMaterials").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Unit_Materials_Desc"
ActiveSheet.ChartObjects("UnitManu").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Unit_Manufacturing_Desc"
ActiveSheet.ChartObjects("UnitSGA").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Unit_SGA_Desc"
ActiveSheet.ChartObjects("UnitEBITDA").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Unit_EBITDA_Desc"
ActiveSheet.ChartObjects("SKUCostStruc").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Unit_EBITDA"
ActiveChart.FullSeriesCollection(2).Values = "==New!Unit_SGA"
ActiveChart.FullSeriesCollection(3).Values = "==New!Unit_Manufacturing"
ActiveChart.FullSeriesCollection(4).Values = "==New!Unit_Materials"
ActiveSheet.ChartObjects("SKUCostStruc100").Activate
ActiveChart.Parent.Delete
ActiveSheet.ChartObjects("SKUCostStruc").Activate
ActiveChart.ChartArea.Copy
Range("AI76").Select
ActiveSheet.Paste
ActiveChart.Name = "SKUCostStruc100"
ActiveSheet.ChartObjects("SKUCostStruc100").Activate
ActiveChart.ChartType = xlColumnStacked100
End Sub
请注意,我已经为图表命名,以防需要在工作表中移动它们。
当宏命中第一个图表数据源更改行时,我收到 "Run-time error 1004: Application defined or object-defined error":
ActiveChart.FullSeriesCollection(1).Values = "==New!EBITDA_Margin"
如果我那么运行
ActiveChart.FullSeriesCollection(1).Values
(选择了 "ProfSKU" 图表)通过立即 window VBA 抛出 "Run-time error 13: Type Mismatch".
我不确定为什么会出现此错误,因为它与我手动录制时生成的代码完全相同。为什么会发生这种情况?
@ShaiRado 只需删除第二个 =
标志即可。我对图表名称 Pareto
的引用返回了一个错误,因为这是一个命名范围,而图表被称为 ParetoChart
。奇怪的是宏录制器会添加一个错误的秒=
。感谢您的帮助。
我正在尝试创建一个宏来重命名选项卡,然后将每个图表的源数据更改为命名范围。我通过 运行ning 通过每个图表记录了一个宏并手动更改源数据并获得了以下代码(通过删除 Application.CutCopyMode = False
和类似代码进行清理):
Sub UpdateRanges()
' UpdateRanges Macro
' Keyboard Shortcut: Ctrl+Shift+J
ActiveSheet.Name = "New"
ActiveSheet.ChartObjects("ProfSKU").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!EBITDA_Margin"
ActiveChart.FullSeriesCollection(2).Values = "==New!Gross_Margin"
ActiveSheet.ChartObjects("Pareto").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Pareto_Revenue"
ActiveChart.FullSeriesCollection(2).Values = "==New!Pareto_EBITDA"
ActiveChart.FullSeriesCollection(3).Values = "==New!Pareto_Volume"
ActiveChart.FullSeriesCollection(4).Values = "==New!Pareto"
ActiveSheet.ChartObjects("UnitMaterials").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Unit_Materials_Desc"
ActiveSheet.ChartObjects("UnitManu").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Unit_Manufacturing_Desc"
ActiveSheet.ChartObjects("UnitSGA").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Unit_SGA_Desc"
ActiveSheet.ChartObjects("UnitEBITDA").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Unit_EBITDA_Desc"
ActiveSheet.ChartObjects("SKUCostStruc").Activate
ActiveChart.FullSeriesCollection(1).Values = "==New!Unit_EBITDA"
ActiveChart.FullSeriesCollection(2).Values = "==New!Unit_SGA"
ActiveChart.FullSeriesCollection(3).Values = "==New!Unit_Manufacturing"
ActiveChart.FullSeriesCollection(4).Values = "==New!Unit_Materials"
ActiveSheet.ChartObjects("SKUCostStruc100").Activate
ActiveChart.Parent.Delete
ActiveSheet.ChartObjects("SKUCostStruc").Activate
ActiveChart.ChartArea.Copy
Range("AI76").Select
ActiveSheet.Paste
ActiveChart.Name = "SKUCostStruc100"
ActiveSheet.ChartObjects("SKUCostStruc100").Activate
ActiveChart.ChartType = xlColumnStacked100
End Sub
请注意,我已经为图表命名,以防需要在工作表中移动它们。
当宏命中第一个图表数据源更改行时,我收到 "Run-time error 1004: Application defined or object-defined error":
ActiveChart.FullSeriesCollection(1).Values = "==New!EBITDA_Margin"
如果我那么运行
ActiveChart.FullSeriesCollection(1).Values
(选择了 "ProfSKU" 图表)通过立即 window VBA 抛出 "Run-time error 13: Type Mismatch".
我不确定为什么会出现此错误,因为它与我手动录制时生成的代码完全相同。为什么会发生这种情况?
@ShaiRado 只需删除第二个 =
标志即可。我对图表名称 Pareto
的引用返回了一个错误,因为这是一个命名范围,而图表被称为 ParetoChart
。奇怪的是宏录制器会添加一个错误的秒=
。感谢您的帮助。