VBA achieves millisecond-level delay (2022 latest version)

foreword

If it weren't for the need to use PPT to draw prizes at the annual meeting, I wouldn't use such difficult-to-use VBA.

To implement the delay function in VBA, most of the tutorials will take the trouble of copying and pasting the ancient posts in ExcelHome in 2016, and then you copy it and find that it cannot run at all.

Now I will tell you from the beginning how to implement the millisecond-level delay function in VBA.

train of thought

The idea is very clear, divided into three steps:

1. Find a way to get the current time (time_now1)

2. Then add a number delay_time to this time (this number is the duration you want to time) to get time_then

3. Then do a loop, and jump out of the loop when the current time (time_now2) is equal to time_then again. And note that it is best not to use the method of wasting computing resources that monopolizes the CPU at this time.

layout

Use a CommandButton1 button control and a Label1 text control to visually show how to implement the delay function in PPT.

the code

Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long
Private Sub CommandButton1_Click()
Dim Savetime As Double
Label1.Caption = "点击开始计时"
Savetime = timeGetTime
While timeGetTime < Savetime + 500
DoEvents
Wend
Label1.Caption = "延时时间到"
End Sub

code analysis

1. Look at the first sentence first:

Private Declare PtrSafe Function timeGetTime Lib "winmm.dll" () As Long

The first sentence is the key content, but Declare is a VBA statement in ancient times, and it is not recommended to be used alone now. It needs to be followed by the PtrSafe keyword. Specific reference: Declare statement (VBA) | Microsoft Docs Learn how to use Declare statement (VBA) https://docs.microsoft.com/zh-cn/office/vba/Language/reference/user-interface-help/declare-statement

The function of this sentence is to take out the timeGetTime in the system DLL file of winmn.dll, which is convenient for later use, and declare it as a Long variable.

2. Look at the second paragraph again:

Private Sub CommandButton1_Click()
Dim Savetime As Double
Label1.Caption = "点击开始计时"
Savetime = timeGetTime
While timeGetTime < Savetime + 500
DoEvents
Wend
Label1.Caption = "延时时间到"
End Sub

Sub...End Sub is a classic event processing function, CommandButton1 is the Button control dragged from above, and CommandButton1_Click() is the action after clicking the Button control.

In the action, first define the temporary variable Savetime of Double type, which is used to temporarily store the time information of the button press, that is, Savetime = timeGetTime

Then make a While...Wend loop, and execute the DoEvents function in this loop. Note that this function is a CPU resource-friendly function, and its function is to hand over execution control so that the operating system can handle other events. Specific reference:

DoEvents function (Visual Basic for Applications) | Microsoft Docs Office VBA reference topic https://docs.microsoft.com/zh-cn/office/vba/language/reference/user-interface-help/doevents-functionHow to break out of the loop That is, the obtained current time is equal to or greater than the value of Savetime + 500, thus realizing timing.

The two lines of code about Label1.Caption = "XXX" just skipped are just to show the running status of the function intuitively, so I won't expand it.

Summarize

To achieve delay, you need to rely on the "three-step" method, and at the same time, pay attention to using the DoEvent function to avoid wasting CPU resources. When using it, just copy the code directly and replace 500 with the desired number of milliseconds.

Guess you like

Origin blog.csdn.net/yuanchenglei/article/details/122332223