Excel宏逐个更新单元格非常慢


' Remove thousand separator from all numeric cells
For each cel in Selection
    if "#,##0" = cel.NumberFormat then _
        cel.NumberFormat = "0"
Next cel

它每秒更新 400 个单元格,虽然它这样做了,但 Excel 没有响应。我怀疑撤消历史记录。有没有办法在某种撤消事务中做到这一点?

根据我的经验,单元格的自动重新计算是几乎所有严重的 excel vba 性能问题背后的罪魁祸首。要测试这是否成立,请执行以下操作:

Application.Calculation = xlCalculationManual
' put your problematic code here
Application.Calculation = xlCalculationAutomatic

如果这没有帮助,另一个可能的罪魁祸首是屏幕更新(尽管由此对性能的影响要轻得多(。尝试:

Application.ScreenUpdating = False
' put your problematic code here
Application.ScreenUpdating = True

希望这对您的情况也有帮助。

您可以在Excel中查找和替换格式。下面是 Excel 2010 帮助中的示例(搜索ReplaceFormat(:

下面的示例设置搜索条件以查找包含 Arial、常规、大小 10 字体的单元格,将其格式替换为 Arial、粗体、大小 8 字体,然后调用 Replace 方法,并将 SearchFormat 和 ReplaceFormat 的可选参数设置为 True 以实际进行更改。

Sub MakeBold()
' Establish search criteria.
With Application.FindFormat.Font
    .Name = "Arial"
    .FontStyle = "Regular"
    .Size = 10
End With
' Establish replacement criteria.
With Application.ReplaceFormat.Font
    .Name = "Arial"
    .FontStyle = "Bold"
    .Size = 8
End With
' Notify user.
With Application.ReplaceFormat.Font
    MsgBox .Name & "-" & .FontStyle & "-" & .Size & _
        " font is what the search criteria will replace cell formats with."
End With
' Make the replacements in the worksheet.
Cells.Replace What:="", Replacement:="", _
    SearchFormat:=True, ReplaceFormat:=True
End Sub

您应该能够修改示例以进行所需的格式更改。

最新更新