日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程资源 > 编程问答 >内容正文

编程问答

Optimize Slow VBA Code

發布時間:2025/3/13 编程问答 37 豆豆
生活随笔 收集整理的這篇文章主要介紹了 Optimize Slow VBA Code 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

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 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 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

總結

以上是生活随笔為你收集整理的Optimize Slow VBA Code的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。

主站蜘蛛池模板: 黄色高清无遮挡 | 一级片在线播放 | 女同调教视频 | 熟女俱乐部五十路六十路av | 国产乱码精品一区二三赶尸艳谈 | 一本色道久久88综合无码 | 久久久久无码精品国产 | 女尊高h男高潮呻吟 | 久久久久久久影院 | 又黄又色又爽 | 日韩欧美一区在线观看 | 肉丝袜脚交视频一区二区 | 五月天久久久久久 | 久久久久国产精品 | 91最新在线| 超碰精品在线观看 | 日韩麻豆 | 欧洲日韩一区二区三区 | 777av | 一本色道久久综合亚洲二区三区 | 国产一区二区三区在线观看 | 亚洲免费网站 | 国产精品9999 | 欧美性插视频 | 国产精品人人做人人爽人人添 | 日韩a级一片 | 国产精品一区二区三区在线看 | 国产原创在线观看 | 国产大片一区二区 | 欧美一区二区三区免费视频 | 欧美xxxx83d| 隔壁人妻偷人bd中字 | 欧美日韩一区二区三区不卡视频 | 侵犯亲女在线播放视频 | 久久免费网| 麻豆传媒在线视频 | 日本www视频在线观看 | 一区二区精品 | 色屁屁一区二区三区视频 | 亚洲国产中文在线 | 亚洲精品久久一区二区三区777 | 欧美激情片一区二区 | 日本人妻熟妇久久久久久 | 亚洲视频一二 | 亚洲日本一区二区 | 三年中文免费观看大全动漫 | 亚洲三级网 | 成人动漫久久 | 2019中文字幕在线视频 | 黑人玩弄人妻一区二区三区影院 | 午夜激情福利电影 | 狠狠欧美 | www.香蕉视频 | 国产精品11 | 诱惑av | 91看片淫黄大片 | 日韩乱码人妻无码中文字幕 | 欧美熟妇另类久久久久久多毛 | 亚洲欧美综合一区二区 | 天天玩天天操 | 久久福利免费视频 | 99re这里只有精品6 | 欧美精品在线一区二区三区 | 午夜影院操 | 精品乱码一区二区三区 | 无码人妻丰满熟妇区五十路 | 久久视频在线播放 | 男女草比视频 | 最新网址av | 日韩伦理一区二区 | 欧美综合在线一区 | 91超薄肉色丝袜交足高跟凉鞋 | av国语| 波多野结衣av中文字幕 | 在线免费观看av网站 | 国产一级片免费播放 | 黄色av免费网站 | 日不卡| 精品国产乱码久久久久久牛牛 | av片久久 | www.色人阁 | 一区二区日韩国产 | 少妇姐姐 | 欧美日韩一区二区三区69堂 | 申鹤乳液狂飙 | 亚洲国产精品无码久久久 | 日本大胆欧美人术艺术 | 欧美日本黄色 | 美国三级视频 | 国产网站免费观看 | 国产视频一区二区在线 | 麻豆www. | 天天插日日干 | 香蕉在线观看视频 | 国产情侣免费视频 | 少妇影院在线观看 | 国产精品xxxxx | 热热色原网址 | 人妻一区二区三区四区 |