Excel VBA中的单元格注释修改命令非常缓慢

我的问题有点宽泛,但我觉得这个问题非常有趣。

有时我必须使用VBA代码创build/清除Excel工作表中的注释,例如:

Range(someRange).ClearComments

要么

.Comment.Shape.Width = 200

与我的代码中的其他复杂计算相比,这些操作花费了大量的时间(如每个1秒)。 每次执行代码时,我都会closuresautomaticCalculationsscreenUpdatingEnableEvents 但这似乎没有什么大不了。 关于如何加快这些行动的任何想法? 在这个非常耗时的命令中,excel是做什么的?

编辑:macros运行在大量数据(100×40,000单元格)的工作表上。 然而执行只需要这个数据的一小部分。 我认为单独执行同一个数据部分,被采用到一个新的工作表的工作要快得多。

Application.ScreenUpdating设置为False 。 请记住当您的macros结束时将ScreenUpdating属性设置为True

如果您多次执行Comment.Shape.Width ,请使用With Statement使VBA避免不必要的path限定。

 With Comment.Shape .Width .Height ' More properties End With 

更多关于如何提高你的macros在这里的速度。

这会提高你的macros的速度,但是,如果你正在处理大量的数据,你会发现一点改善。 那么优化你的代码就好多了。

我使用这个例子,它在列A中添加了一个引用列B的单元格注释,我在列B中填充了1500行,并运行了约4秒的代码。

Delete_Comment代码是即时的。

 Sub Add_Comment() Dim Rws As Long, Rng As Range, c As Range Rws = Cells(Rows.Count, "B").End(xlUp).Row Set Rng = Range(Cells(2, 1), Cells(Rws, 1)) For Each c In Rng.Cells With c .ClearComments .AddComment .Comment.Visible = False .Comment.Text c.Offset(0, 1).Value .Comment.Visible = False .Comment.Shape.TextFrame.AutoSize = True End With Next c End Sub Sub Delete_Comment() Dim Rws As Long, Rng As Range Rws = Cells(Rows.Count, "B").End(xlUp).Row Set Rng = Range(Cells(2, 1), Cells(Rws, 1)) Rng.ClearComments End Sub 

我遇到了同样的问题,发现它可能是Shape.AutoSize属性。 当设置为真时,Excel处理评论的速度明显变慢。 在我的情况下,只有每秒约4。