博客
关于我
强烈建议你试试无所不能的chatGPT,快点击我
Optimize Slow VBA Code
阅读量:5275 次
发布时间:2019-06-14

本文共 3806 字,大约阅读时间需要 12 分钟。

Speed up code and stop screen flickering:

Sub NoScreenRePainting()     Application.ScreenUpdating=False   'Your code here.     Application.ScreenUpdating=True End Sub

Preventing calculation while executing code:

Sub NoCalculations()   Application.Calculation = xlCalculationManual   'Your code here.   Application.Calculation = xlCalculationAutomatic End Sub

Speeding up code if you have Worksheet or Workbook Events. Also stops endless loops in Events

Sub StopAllEvents()   Application.EnableEvents = False   'Your code here.   Application.EnableEvents = True End Sub

Use the With Statement when working with Objects.

Sub WithARange()   With Range("A1")     .Value = 100     .Font.Bold = True     .Interior.ColorIndex = 6     .Copy Destination:=Range("B1")   End With End Sub

Use VbNullString instead of = "" When needing to default a String variable back to it's default of "" use:

Sub EmptyText()   Dim strWords As String   strWords = "Cats"   MsgBox strWords   strWords = vbNullString   MsgBox strWords End Sub

Inserting a Relative formula into a range of cells: Faster than AutoFill or Copy.

Sub NoAutoFillOrCopy()   Range("A1:A200").FormulaR1C1 = "=SUM(RC[1]:RC[5])" End Sub

Tip: To get a formula, type it in any cell then select the cell, go Tools>Macro>Record new macro and record a macro pushing F2 then Enter.

Avoid the use of Copy and Paste whenever Possible:

Sub NoCopyAndPaste()   'Instead of:   Sheet1.Range("A1:A200").Copy   Sheet2.Range("B1").pasteSpecial   Application.CutCopyMode=False'Clear Clipboard   'Use:   'By-passes the Clipboard   Sheet1.Range("A1:A200").Copy Destination:=Sheet2.Range("B1")   'Or, if only values are needed:   Sheet2.Range("B1:B200").Value= Sheet1.Range("A1:A200").Value   'Or, if only formulae are needed:   Sheet2.Range("B1:B200").Formula = Sheet1.Range("A1:A200").Formula   'See also FormulaArray and FormulaR1C1 etc   'Instead of:   Sheet1.Range("A1:A200").Copy   Sheet1.Range("A1:A200").PasteSpecial xlPasteValues   Application.CutCopyMode=False'Clear Clipboard   'Use:   Sheet1.Range("A1:A200") = Sheet1.Range("A1:A200").Value End Sub

Always declare your variables correctly!

To quickly view a variables definition:
Select the variable that you want the definition for.
Go to View>Definition (Shift+F2)
To return to your previous position:
Go to View>Last Postition (Ctrl+Shift+F2).

Release memory from Object variables:

Sub ReleaseObjectMemory()   'Could be any variable of the Object type   Dim wSheet as Worksheet   'Set Object variable   Set wSheet = Sheet1   'Your code here.   'Release memory   Set wSheet = Nothing End Sub

Don't get caught in the Loop.

Follow to see why Loops should (and usually can) be avoided.

Avoid If, Else whenever possible

More often than not people would use an If, Else Statement to test whether a condition is TRUE or FALSE. There is however a slightly faster (and less typing) method. The first example shows the common method, while the second shows a faster method. Of course in such a small example the difference is not noticeable.

Sub TrueOrFalseSlower()   Dim bYesNo As Boolean   Dim i As Integer   If i = 5 Then 	bYesNo = True   Else 	bYesNo = False   End If   MsgBox bYesNo End Sub

Here's a better way!

Sub TrueOrFalseFaster()   Dim bYesNo As Boolean   Dim i As Integer   bYesNo = (i = 5)   MsgBox bYesNo End Sub

Another common need is to toggle a variable between True and False depending on a condition. For example:

Sub ToggleTrueOrFalseSlower()   Dim bYesNo As Boolean   If bYesNo = False Then 	bYesNo = True   Else 	bYesNo = False   End If   MsgBox bYesNo End Sub

Here's a much better way

Sub ToggleTrueOrFalseFaster()   Dim bYesNo As Boolean   bYesNo = Not bYesNo   MsgBox bYesNo End Sub Source: http://www.ozgrid.com/VBA/SpeedingUpVBACode.htm

转载于:https://www.cnblogs.com/end/archive/2010/11/05/1869819.html

你可能感兴趣的文章
shell 输入输出重定向
查看>>
maven工程搭建
查看>>
【leetcode 简单】第十一题 搜索插入位置
查看>>
AS3中 is,as,typeof的区别 .
查看>>
排序算法总结
查看>>
Android 开发笔记___初级控件之实战__计算器
查看>>
工具类--常量类
查看>>
关于hbase的read操作的深入研究 region到storefile过程
查看>>
《浪潮之巅》读书笔记(中)
查看>>
Codeforces 989C (构造)
查看>>
[zz] C++智能指针循环引用解决
查看>>
用Navicat连接Oracle数据库时报错ORA-28547:connection to server failed, probable Oracle Net admin error...
查看>>
PHP之PHP文件引用详解
查看>>
手脱ASProtect v1.23 RC1(无Stolen Code)
查看>>
python 解析XML
查看>>
websocket vs keep-live
查看>>
js 调用父窗口的方法
查看>>
JSONP的诞生、原理及应用实例
查看>>
oracle时间运算
查看>>
关于MySQL的各种总结
查看>>