简体   繁体   English

VBA - Excel:如何优化此代码?

[英]VBA - Excel: How can I optimize this code?

For a long time I'm not playing with VBA, so we have a spreadsheet on my work and checkng its code, I'm sure it can be improved.很长一段时间我都没有使用 VBA,所以我们有一个关于我的工作的电子表格并检查它的代码,我相信它可以改进。

Basically this spreadsheet has literally 200 buttons (100 to open and another 100 to close) and it copies the data from one sheet to another.基本上这个电子表格有 200 个按钮(打开 100 个,关闭另外 100 个),它将数据从一张纸复制到另一张纸。 Below are the examples of two of this macros.下面是其中两个宏的示例。

Macro #1:宏 #1:

Sub IT100stop()
'
' newstop Macro
'
' Keyboard Shortcut: Ctrl+s
'
    Application.ScreenUpdating = False
    Range("G47").Select
    ActiveCell.FormulaR1C1 = "DOWN"
    
    
    Range("H47").Select
    ActiveCell.FormulaR1C1 = _
        "=YEAR(TODAY())&MONTH(TODAY())&DAY(TODAY())&HOUR(NOW())&MINUTE(NOW())&SECOND(NOW())"
    Range("H47").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Range("j47").Select
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=NOW()"
    Range("j47").Select
    Selection.Copy
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    
    Range("K47").Select
    Application.CutCopyMode = False
    Application.CutCopyMode = False
    ActiveCell.FormulaR1C1 = "=IF(RC[-1]="""","""",IF(NOW()-RC[-1]<1,HOUR(NOW()-RC[-1])&"" h ""&MINUTE(NOW()-RC[-1])&"" m"",IF(DAYS(NOW(),RC[-1])<2,DAYS(NOW(),RC[-1])&"" day"",DAYS(NOW(),RC[-1])&"" days"")))"

    Range("F47").Select
Application.ScreenUpdating = True
End Sub

Macro #2:宏 #2:

Sub IT100released()
'
' newreleased Macro
'
' Keyboard Shortcut: Ctrl+r
'
Application.ScreenUpdating = False
    
    Sheets("Database").Select
    Range("A2").Select
    Application.CutCopyMode = False
    Rows("2:2").Select
    Selection.Insert shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
    Sheets("SINOPTIC").Select
    Range("F47:U47").Select
    Selection.Copy
    Sheets("Database").Select
    Range("A2").Select
    Selection.PasteSpecial Paste:=xlPasteValuesAndNumberFormats, Operation:= _
        xlNone, SkipBlanks:=False, Transpose:=False
    Sheets("SINOPTIC").Select
    Range("G47").Select
    ActiveCell.FormulaR1C1 = "OK"
    Range("H47:U47").Select
    Selection.ClearContents
Application.ScreenUpdating = True
End Sub

The question is: what can we do to improve this code?问题是:我们可以做些什么来改进这段代码? If I add this following code before and after the actual macro code, will the calculations be faster?如果我在实际宏代码前后添加以下代码,计算速度会更快吗?

Application.Calculation = xlCalculationManual
Application.ScreenUpdating = False
Application.DisplayStatusBar = False
Application.EnableEvents = False

'Macro Code

Application.EnableEvents = True
Application. DisplayStatusBar = True
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic

Thanks a lot!非常感谢!

Pay attention to what BigBen wrote: avoid the Select;注意 BigBen 写的:避免选择; that is the code created by the macro recorder, but it performs many unnecessary operations.这是宏记录器创建的代码,但它执行了许多不必要的操作。 This is the "human" version of your macro # 2这是您的宏 #2 的“人类”版本

Sub IT100released()
'
' newreleased Macro
'
' Keyboard Shortcut: Ctrl+r
'
    With Application
        .Calculation = xlCalculationManual
        .ScreenUpdating = False
        .EnableEvents = False
        Sheets("Database").Rows("2:2").Insert shift:=xlDown, CopyOrigin:=xlFormatFromLeftOrAbove
        Sheets("SINOPTIC").Range("F47:U47").Copy
        Sheets("Database").Range("A2").PasteSpecial Paste:=xlPasteValuesAndNumberFormats
        Sheets("SINOPTIC").Range("G47") = "OK"
        Sheets("SINOPTIC").Range("H47:U47").ClearContents
        .EnableEvents = True
        .ScreenUpdating = True
        .Calculation = xlCalculationAutomatic
    End With
End Sub

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM