vba 用于创建图表的 Excel 2010 宏

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11664246/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-11 17:03:16  来源:igfitidea点击:

Excel 2010 Macro to Create Charts

excelvbaexcel-vbachartsbuilding

提问by Adena Goldrich

I am trying to create macros to build charts in Excel 2010.

我正在尝试创建宏以在 Excel 2010 中构建图表。

If I record a macro it gives me the following code:

如果我录制一个宏,它会给我以下代码:

ActiveSheet.Shapes.AddChart.Select
ActiveChart.SetSourceData Source:=Range("'Sheet1'!$A:$I")
ActiveChart.ChartType = xlColumnStacked

But when I rerun it, it gives me the following error:

但是当我重新运行它时,它给了我以下错误:

Run time error '1004': Application defined or object defined error.

运行时错误“1004”:应用程序定义或对象定义错误。

I have tried many different variations:

我尝试了许多不同的变体:

With ActiveSheet.ChartObjects.Add _
        (Left:=100, Width:=690, Top:=75, Height:=375)
    With .Chart
       .Parent.Name = "Chart MVO"
       .HasTitle = True
       .ChartTitle.Text = "MVO (628) CF and DS Delivery Performance"
    End With
 End With

But each gives a different run time error. What am I doing wrong?

但是每个都会给出不同的运行时错误。我究竟做错了什么?

回答by martin

Didn't you rename your sheet? Are you sure that you have a sheet active? When I try your code it runs without problem.

你没有重命名你的工作表吗?您确定您的工作表处于活动状态吗?当我尝试您的代码时,它运行没有问题。

When you run your code and it gives you an error, choose Debug in the window that appears and check what line it crashes on. Then hover your mouse over different variables on that line and see if all of them are valid.

当您运行代码并出现错误时,请在出现的窗口中选择 Debug 并检查它崩溃的行。然后将鼠标悬停在该行上的不同变量上,看看它们是否都有效。