[VBA] Enable sleep to be used

スポンサーリンク
excelvba excel
スポンサーリンク

VBA does not have a sleep command.

Therefore, we need to use the Windows API to be able to use sleep within VBA.

Add the following to the top of the module (above the function) to declare the API.

#If VBA7 Then
	Private Declare PtrSafe Sub Sleep Lib "kernel32" (ByVal ms As LongPtr)
#Else
	'32bit版
	Private Declare Sub Sleep Lib "kernel32" (ByVal ms As Long)
#End If

The upper part is for 64 bit, and the lower part is for 32 bit (other than 64 bit).

sub sleep_test()
	sleep 1000 'Millisecond 1 second = 1000
end sub

This will result in a function that does nothing for 1 second and then exits.

However, waiting for processing in seconds should not be abused.

When you wait in seconds, if you use it to wait for some kind of processing, it may not work as expected depending on the load status of the PC.

Ideally, the process flow should be clearly determined, and the structure should be such that the process is confirmed to be completed before proceeding to the next process.

コメント

Copied title and URL