VBA图表操作缓慢

我写了一些生成散点图的Excel VBA代码,并更改了图表的一些属性。 (代码如下,以供参考。)代码缓慢移动,如删除图表图例,删除水平/垂直网格线,以及更改X和Y系列。 Excel的计时器为每个任务提供了以下持续时间:

insert scatterplot: 0.01171875 delete series: 0 plot x vs y: 0.55859375 delete legend: 0.5703125 delete chart title: 0.66015625 remove grid: 1.3046875 format axes: 0 overall: 3.11328125 

删除网格,改变标题,绘制X和Y系列,删除图例似乎需要很长时间。 我已经search了替代方法来编写代码,但一直没有find有用的东西。 代码完全按预期工作,除了速度慢以外。 任何想法是什么导致了糟糕的performance,以及如何加快这一点? 提前致谢。

编辑:我已经closures屏幕更新,同时使用图表。 图表是在用户表单打开的情况下生成/格式化的,如果这有什么区别的话。

以下是相关的代码片段:

 With ActiveChart 'Delete all series currently in plot Do While .FullSeriesCollection.Count > 0 .FullSeriesCollection(1).Delete Loop 'Plot Actual (Y) vs. Inverse Distribution (X) .SeriesCollection.NewSeries .FullSeriesCollection(1).XValues = "=" & tempSheetName & "!$C:$C" .FullSeriesCollection(1).Values = "=" & tempSheetName & "!$A:$A" 'Delete legend .Legend.Delete 'Delete chart title .SetElement (msoElementChartTitleNone) 'Remove gridlines .SetElement (msoElementPrimaryValueGridLinesNone) .SetElement (msoElementPrimaryCategoryGridLinesNone) 'Format axes Dim xAxis As Axis, yAxis As Axis Set xAxis = .Axes(xlCategory) Set yAxis = .Axes(xlValue) With yAxis 'Title y axis "actual" .HasTitle = True .AxisTitle.Caption = "Actual" 'Add tick marks .MajorTickMark = xlOutside End With With xAxis 'Title x axis by dist type .HasTitle = True .AxisTitle.Caption = dist.getDistType 'Add tick marks .MajorTickMark = xlOutside End With End With 

没有数据和机器细节,可能很难说为什么这是缓慢的,虽然这是一些代码的一些替代品。

我要改变的第一件事也不是激活图表。 如果要通过代码创build图表,请将其设置为variables,例如Set wcChart = ThisWorkbook.Charts.Add 。 然后With ActiveChart更改为With wcChart

另外,删除FullSeriesCollection ,然后删除图表标题,删除网格线并在填充新数据之前更改坐标轴。 图表操作应该更快,图表中的数据更less。 这里要小心,因为不同顺序图表的变化产生不同的输出(例如图例的布局)。

使用A和C的整个列填充新的FullSeriesCollection ,指定数据的确切范围,而不是整列。

其他的尝试,我不是说这些会起作用,但如果你还没有尝试过,那么他们是值得一试的。 而不是每次都检查FullSeriesCollection

 Do While .FullSeriesCollection.Count > 0 .FullSeriesCollection(1).Delete Loop 

以下可能会更快:

 For ii = .FullSeriesCollection.Count To 1 Step -1 .FullSeriesCollection(ii).Delete Next ii 

此外,而不是.SetElement图表标题和网格线我使用以下内容:

 'You have to set the title to 'True' before it'll work with 'False'. Go figure. .HasTitle = True .HasTitle = False .HasMajorGridlines = False .HasMinorGridlines = False