如何加速修剪宏

时间:2016-11-29 16:11:18

标签: excel-vba trim vba excel

我的部分宏使用Trim功能。它恰好是我的宏中最慢的部分,我正在寻求加快速度。有没有人建议某些代码可以实现相同目的,但更快?

Sub Trim()

For Each c In Sheets("Approved Closing Data Draw").UsedRange c.Value = WorksheetFunction.Trim(c.Value) Next c

For Each c In Sheets("Pipeline - Underwriting Data D").UsedRange c.Value = WorksheetFunction.Trim(c.Value) Next c

For Each c In Sheets("Modifications").UsedRange c.Value = WorksheetFunction.Trim(c.Value) Next c

For Each c In Sheets("Lead Data").UsedRange c.Value = WorksheetFunction.Trim(c.Value) Next c

Sheets("Approved Closing Data Draw").Select

End Sub

2 个答案:

答案 0 :(得分:0)

两个主要的事情是关闭计算和屏幕更新。您还可以尝试其他一些事情。请参阅以下链接。

https://www.soa.org/News-and-Publications/Newsletters/Compact/2012/january/com-2012-iss42-roper.aspx

答案 1 :(得分:0)

这应该更快:

PreferenceManager prefMngr = getPreferenceManager();
prefMngr.setSharedPreferencesName("my_name");

仅使用Sub DoSheets() TrimUsedRange Sheets("Approved Closing Data Draw") TrimUsedRange Sheets("Pipeline - Underwriting Data D") 'etc End Sub Sub TrimUsedRange(sht As Worksheet) Dim d, r As Long, c As Long With sht.UsedRange If .CountLarge = 1 Then 'handle empty sheets .Value = WorksheetFunction.Trim(.Value) Else d = .Value For r = 1 To UBound(d, 1) For c = 1 To UBound(d, 2) d(r, c) = WorksheetFunction.Trim(d(r, c)) Next c Next r .Value = d End If End With End Sub 可能比Trim()

更快