【问题标题】:Avoiding the use of Activate in Excel VBA for Chart避免在 Excel VBA 中为图表使用激活
【发布时间】:2017-02-09 19:52:26
【问题描述】:

这里对 Excel VBA 非常陌生...与这篇文章非常相似:Avoiding the use of Activate and Select when working with charts (Excel),我试图避免使用激活,以便对大型工作簿中的许多图表进行小幅更改。我正在为大量图表执行此任务,因此我想尽可能减少运行时间,并且听说选择/激活函数往往会减慢宏。

我的示例代码如下所示:

ActiveSheet.ChartObjects("Chart 3").Activate
ActiveChart.FullSeriesCollection(1).XValues = "=Leb!$C$2:$G$2"

我尝试将这两行替换为以下内容,因为这对我来说合乎逻辑:

ActiveSheet.ChartObjects("Chart 3").FullSeriesCollection(1).XValues = "=Leb!$C$2:$G$2"

此代码在运行时会导致错误,因此我尝试使用上述帖子中显示的 WITH 格式:

With Worksheets("Chart").ChartObjects("Chart 3").Chart.XValues = "=Leb!$C$2:$G$2"
End With

这也会引发错误。

我知道我可能犯了一个小的语法错误,但是任何人都能够提供任何关于我做错了什么的见解吗? (我以前从未使用过 WITH 函数,因此对它进行一些一般性的了解也会很有帮助。)

提前致谢。

【问题讨论】:

  • 您需要在 ChartObjectsFullSeriesCollection 之间包含关键字 ChartActiveSheet.ChartObjects("Chart 3").Chart.FullSeriesCollection(1).XValues = "=Leb!$C$2:$G$2"

标签: vba excel


【解决方案1】:

差不多了……

With Worksheets("Chart").ChartObjects("Chart 3").Chart
    .SeriesCollection(1).XValues = "=Leb!$C$2:$G$2"
End With

XValues 属于 Series 对象,而不是 Chart(参考您的上一个代码示例)。

编辑:

Dim arrCharts, cht
arrCharts = Array("Chart 3", "Chart 4", "Chart 5", "Chart 7")

For Each cht In arrCharts
    With Worksheets("Chart").ChartObjects(cht).Chart
        .SeriesCollection(1).XValues = "=Leb!$C$2:$G$2"
    End With
Next cht

【讨论】:

  • 谢谢!是否也可以对同一张纸上的多个图表进行这种精确的更改?我试过With Worksheets("Dash Board").ChartObjects("Chart 3", "Chart 4", "Chart 5", "Chart 7").Chart.SeriesCollection(1).XValues = "=Leb!$C$2:$G$2"End With
  • @TimWilliams 你的意思是SeriesCollection 属于Chart,而不是ChartObject
  • @Tim Williams 再次感谢!我不得不将第二个“结束于”更改为“下一个 cht”,但它现在可以完美运行 :)
猜你喜欢
  • 2013-12-27
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-11-26
  • 1970-01-01
相关资源
最近更新 更多