我正在尝试创建一个宏,它将重命名选项卡,然后将每个图表的源数据更改为命名范围。我通过遍历每个图表并手动更改源数据来记录宏,并获得以下代码(通过删除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
请注意,我已经命名了图表,以防我需要在工作表中移动它们。
我收到"运行时错误1004:应用程序定义或对象定义错误"当宏命中第一个图表数据源更改行时:
ActiveChart.FullSeriesCollection(1).Values = "==New!EBITDA_Margin"
如果我再运行
ActiveChart.FullSeriesCollection(1).Values
(选择" ProfSKU"图表)通过立即窗口VBA抛出"运行时错误13:输入不匹配"。
我不确定为什么会出现此错误,因为它是我手动记录时生成的完全相同的代码。为什么会发生这种情况?
答案 0 :(得分:0)
@ShaiRado删除第二个$match[1] = preg_replace_callback('/(?<=^|[\x09\x20\x2D])./e',
function ($matches) {
foreach ($matches as $match) {
return strtoupper($match);
}
},
strtolower(trim($match[1])));
符号就是所需要的。我对图表名称=
的引用返回了一个错误,因为这是一个命名区域,而图表名为Pareto
。奇怪的是,宏录制器会添加错误的第二ParetoChart
。谢谢你的帮助。