Performance VBA图表操作速度慢

Performance VBA图表操作速度慢,performance,vba,excel,Performance,Vba,Excel,我编写了一些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 gr

我编写了一些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系列以及删除图例似乎需要很长时间。我在谷歌上搜索过其他编写代码的方法,但没有找到任何有用的方法。除了速度慢之外,代码完全按照预期工作。关于是什么导致了糟糕的性能,以及我如何加快速度,有什么想法吗?提前谢谢

编辑:我已经在使用图表时关闭了屏幕更新。如果用户窗体处于打开状态,则会生成/格式化图表

以下是相关的代码片段:

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

如果没有特定的数据和机器,可能很难说为什么这样做很慢,尽管这里有一些代码的替代方案

我要改变的第一件也是最重要的事情是不要激活图表。如果您是通过代码创建图表,请这样做,但要将其设置为变量,例如
set wcChart=thishworkbook.Charts.Add
。然后将使用ActiveChart的
更改为使用wcChart的

另外,在填充新数据之前,删除
FullSeriesCollection
,然后删除图表标题,删除网格线并更改轴。图表中的数据越少,图表操作应该越快。但在这里要小心,因为以不同顺序更改图表的各个方面可能会产生不同的输出(例如图例的布局)

使用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

你能发布你的工作手册的样本吗?谢谢你的建议——我已经能够将整个过程从之前的3秒缩短到0.5秒。这里的关键建议似乎是最后绘制X和Y系列,并在图表中没有数据的情况下操作图表。现在运行时,作为参考:
insert scatterplot:0.05078125 delete series:0 delete legend:0 delete chart title:0 format axes:0 plot x vs y:0.5234375总体:0.578125
此外,在删除图表标题时,我可以使用
.HasTitle=False
,而不必先设置为true,也没有问题。还应注意的是,
.hasmayorgridlines
.HasMinorGridlines
轴的属性,而不是
图表的属性。很高兴听到这一点起作用,您必须有合理的数据点才能使其减速。您需要
.hastintle=False/True
才能向后兼容至少2007年(可能是2010年?)。用网格线就够了。