Speed up code and stop screen flickering:

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

Preventing calculation while executing code:

Sub NoCalculations()  Application.Calculation = xlCalculationManual  'Your code here.  Application.Calculation = xlCalculationAutomaticEnd 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 = TrueEnd 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 WithEnd 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 strWordsEnd 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").ValueEnd 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 = NothingEnd Sub

Don't get caught in the Loop.
Follow this link 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 bYesNoEnd Sub

Here's a better way!

Sub TrueOrFalseFaster()  Dim bYesNo As Boolean  Dim i As Integer  bYesNo = (i = 5)  MsgBox bYesNoEnd 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 bYesNoEnd Sub

Here's a much better way

Sub ToggleTrueOrFalseFaster()  Dim bYesNo As Boolean  bYesNo = Not bYesNo  MsgBox bYesNoEnd Sub

Source: http://www.ozgrid.com/VBA/SpeedingUpVBACode.htm

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

Optimize Slow VBA Code相关推荐

  1. ArcGIS Field Calculator 计算,错误 “Error running VBA code: 语句未结束” 解决方法

    在ArcGIS 中的Field Calculator ,当使用 VBA script 进行计算时,出现错误 "Error running VBA code: 语句未结束". 解决办 ...

  2. Code VBA完整代码块,创建Visual Basic源代码

    Code VBA完整代码块,创建Visual Basic源代码 Code VBA是一个外接程序,其中包含一组Microsoft Office工具,旨在使开发人员更容易创建Visual Basic源代码 ...

  3. 如何避免在Excel VBA中使用选择

    本文翻译自:How to avoid using Select in Excel VBA I've heard much about the understandable abhorrence of ...

  4. access 导入 txt sql语句_access将SQL语句从查询复制到VBA

    在access vba中,因为要对表中数据的操作,编写代码经常需要调用SQL语句. 我们通常不会在VBA代码中键入复杂的查询语句,而是在查询的设计视图中创建一个查询,将其切换到SQL视图,复制并粘贴到 ...

  5. 用VBA生成PDF Adobe Acrobat and VBA – An Introduction

    Here is another topic that comes up every now and then: How can I "talk" to Adobe Acrobat ...

  6. excel 透视表 vba_使用Excel VBA删除数据透视表计算字段

    excel 透视表 vba Yesterday, I started out with the best of intentions, planning to get some work done, ...

  7. Excel VBA教程–如何使用Visual Basic在电子表格中编写代码

    介绍 (Introduction) This is a tutorial about writing code in Excel spreadsheets using Visual Basic for ...

  8. k3v12.0精益版注册机_精益均值VBA机

    k3v12.0精益版注册机 This is Recruit a New VBA Programmer Week, according to Dick Kusleika, so we'd better ...

  9. AutoCAD VBA enabler 2010-2017

    官方说法是VBA6的发布授权已经结束,AutoDesk 只能继续发布AutoCAD 2014及以后的支持VBA7.1的版本. 搜了下,之前的虽然不公开,但是链接仍然能找到. AutoCad VBA 2 ...

最新文章

  1. mysql编辑表php源码_MySQL修改表的实际应用代码示例
  2. SSM-Spring+SpringMVC+MyBatis整合案例从0到1
  3. Tkinter的entry组件
  4. Log4j、slf4j
  5. 【计算机组成原理】补码的加减运算方法
  6. mysql数据库邮箱什么类型_MySQL的数据类型介绍
  7. matlab 码元扩展,扩频通信及matlab仿真
  8. 2020 AI交流大会 | ALBERT、多模态内容生成、京东零售CV实践、Bias的解决
  9. 【Xamarin.iOS】使用iOS 11进行大型游戏
  10. Thinkpad x230 win7/xp 双系统安装全过程
  11. Tableau Desktop 10.4.2 的安装和激活
  12. 硬币的一分、二分与五分有收藏价值吗,都是怎样的?
  13. React基础之事件机制
  14. Enhancement spot 的实现
  15. python与jay的龙卷风
  16. vscode下载太慢,快速下载vscode方法!
  17. 我国开始研制电子计算机,我国从( )年开始研制电子计算机。
  18. 基于SmartThreadPool线程池技术实现多任务批量处理
  19. 记录一次前途迷茫的选择
  20. js splice,slice,split区别

热门文章

  1. linux下线程不需要死循环么,Linux多线程程序死循环问题调试
  2. JavaScript怎么安装_几句话说清楚JavaScript、V8引擎、NodeJS、NMP,到底是什么东东...
  3. part.write java_小白向:web中利用request.getPart()上传文件到服务器
  4. Tex2Word vs Word2Tex
  5. 周志华眼中的贝叶斯方法(转)
  6. 路由器安置(Routing)
  7. linux异地文件同步软件,rsync完成异地文件的同步
  8. java实现微信创建菜单_java实现微信公众平台自定义菜单的创建示例
  9. 去重和分类后缀asp、php等路径 用python3写的
  10. PythonNote01_HTML标签