Why is VBA code faster when called from a standard module (instead of a custom form)?

Alternative name: why pressing Escmakes my MS Word macro faster

While waiting for some code to run, I came across something interesting.

The code is slower ... click the Esccode is fast. Example:

  • press Escright after execution - 2 seconds to complete
  • do not click Escon all - up to 30 seconds to complete

It makes no sense to me. It turns out that other people have noticed similar behavior, for example:

And they found various solutions or workarounds. However , these messages are for MS-Excel; which apparently has different behavior for the key Esc.

In MS-Excel, a keystroke Esccan (depending on the Application.EnableCancleKey parameter) interrupt the code or cause an error (Err 18) or do nothing. On the other hand , Application.EnableCancleKey changes the Ctrl+ behavior instead Pause. Nevertheless, in spite of this, keystroke Escsignificantly speeds up code execution.

In contrast to this question, my question is more about placing code in a user form. For example, in my Userform:

Private Sub Cmd_Click()

    Module1.Macro1
    Module1.Macro2
    Module1.Macro3

End Sub

64- Word 2010, , :

UserForm:

Private Sub Cmd_Click()

    Module1.RunMacro123

End Sub

:

Private Sub RunMacro123()

    Module1.Macro1
    Module1.Macro2
    Module1.Macro3

End Sub

:

  • 64- MS Word, 32- , , ( )
  • , ..
  • , :
    • Esc
  • Macro1, Macro2 Macro3 (FWIW) INI

, sendKeys Esc, .

- :

, , : http://www.tek-tips.com/viewthread.cfm?qid=1468970

"" (debug.print timer - startTime) , , reset (startTime = timer) . NotePad ++

, , , ~ 0,04 ( NB, = ).

. , , - , . Comparing runtime code in NotePad ++

- , , getStyleElement, , . , , .

, applyStyleFormat ( getStyleElement).

- With For Loop; - :

For i = 1 to Styles.Count
    With aDocument.Styles(i)
        .Font.??? = Something
        ' or .Paragraph.??? = Something
    End With
Next i

, - Esc, , , - ...

+4
1

@Florent Bs, , click? ,

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

'code
Module1.Macro1
Module1.Macro2
Module1.Macro3

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

, , ? , .

0

Source: https://habr.com/ru/post/1682311/


All Articles