Excelmacros更新单元格一个接一个是非常慢

' 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中find并replace格式。 以下是Excel 2010帮助(searchReplaceFormat )的示例:

下面的示例将search标准设置为查找包含Arial,Regular,Size 10字体的单元格,用Arial,Bold,Size 8字体replace它们的格式,然后调用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 

你应该可以修改这个例子来改变你所需要的格式。

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

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

如果这没有帮助,另一个可能的罪魁祸首是screenupdating(虽然由此产生的性能不那么严重)。 尝试:

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

希望这也有助于你的情况。