记录的宏在图表对象上不起作用

记录的宏在图表对象上不起作用

问题描述:

我记录了以下宏:

Sheets("Rejets Techniques TGC").Select
ActiveSheet.ChartObjects("Graphique 1").Activate
ActiveChart.Axes(xlCategory).Select
ActiveChart.SeriesCollection(1).Values = "='Données'!$EU$68:$IJ$68"
ActiveChart.SeriesCollection(1).XValues = "='Données'!$EU$1:$IJ$1"

但是,当我尝试拉扯它时,我收到这个错误(从法语翻译):

However when I try to lauch it I get this error (translated from french):

Execution error '-2147024809 (80070057)'
There is no element with this name

这怎么可能?如果没有以这种方式命名的图形,我将不能
记录。
(是的,我从好的表中运行)

How can this be? if there was no graph named this way I wouldn't have been able to record it. (yes I'm running it from the good sheet)

谢谢。

下面是这样的:您的图表不是表单上的对象

Here's what it comes down to: Your chart is not an object on the sheet, it is the sheet.

所以当你使用 ActiveSheet.ChartObjects(Graphique 1)。激活启动你的代码,没有 ChartObjects ,因为工作表是 Chart 。所以这里是你如何解决:

So while you use ActiveSheet.ChartObjects("Graphique 1").Activate to start your code, there are no ChartObjects found in your sheet, because the sheet is the Chart. So here's how you get around it:

Dim CO As Variant
Set CO = ActiveSheet
CO.Axes(xlCategory).Select
CO.SeriesCollection(1).Values = "='Données'!$ET$68:$IJ$68"
CO.SeriesCollection(1).XValues = "='Données'!$ET$1:$IJ$1"

这应该可以正常工作。我注意到,当我看着图表选项卡,我无法进入任何单元格。这不是异常的,但它不是最常见的方式(我看到)创建图表。要验证,我添加了一个手表 ActiveSheet ,看到它确实是一个图表(类型为 Object / Graph2 ),所有的普通图表方法都可用它。

And this should work just fine. I noticed that when I looked at the chart tab, I couldn't get into any cells. This is not abnormal, but it is not the most common way (that I see) to create the chart. To verify, I added a watch on the ActiveSheet and saw that it was indeed a chart (of type Object/Graph2) with all the normal chart methods available to it.

从那里,我只是插入你的代码,转换为 CO 变量(但你的应该仍然使用 ActiveSheet ,并且没有错误。

From there, I just plugged in your code, converting to the CO variable (but yours should still work using ActiveSheet across the board), and ran with no errors.

作为附注,使用 ActiveSheet 并不总是有效,并且通常更好的是显式调用该表,即 Set CO = ThisWorkbook.Sheets (Rejets Techniques TGC)

As a side note, using ActiveSheet is not always effective, and it is generally better to explicitly call the sheet, i.e. Set CO = ThisWorkbook.Sheets("Rejets Techniques TGC")