マクロコードを一時停止するExcelVBAスリープ関数

ExcelVBAスリープ機能

VBAスリープ関数は、Windows DLLファイルの下にあるWindows関数であり、プログラムを再開できる一定の時間の後、マクロプロシージャの実行を指定された時間停止または一時停止するために使用されます。

他の一連のタスクを完了するために、マクロ実行プロセスを一時停止する必要がある場合があります。他のタスクのセットは、コーディングの一部または他のマクロプロシージャの一部であるか、現在のExcelマクロに入力することができます。プログラムの実行中にプログラムを一時停止するにはどうすればよいですか?ユーザーが指定した時間、手順のコードを一時停止し、その後、プログラムを再開することができます。SLEEP関数を使用してVBAでこれを行うことができます。

VBAスリープ機能は何をしますか?

名前自体が「しばらく眠る」、「しばらく休む」、「しばらく一時停止する」、しばらく休む」などと言うように、SLEEP。スリープ機能を使用すると、ユーザーはマクロコードをミリ秒単位で一時停止できます。これを使用すると、マクロコードのプロセスを遅らせることができます。

SLEEPという組み込み関数があると思うなら、それは間違いです。VBAにはそのような関数がなく、Windows関数としてSleepという関数があるからです。特別なコードセットを入力することで、VBAでこの関数を実際に呼び出すことができます。実際、これはWindows DLLファイル内に存在する関数であるため、vbaでサブルーチンを開始する前にAPIの命名法を宣言する必要があります。

以下はVBAコードです。

コード:

#If VBA7 Then Public Declare PtrSafe Sub Sleep Lib "kernel32"(ByVal dwMilliseconds As LongPtr)'64ビットバージョンのExcelの場合#Else Public Declare Sub Sleep Lib "kernel32"(ByVal dwMilliseconds As Long)'32ビットバージョンの場合Excel#End If 

マクロコードの記述を開始する前に、上記をコピーしてモジュールに貼り付けてください。このようにモジュールに貼り付ける必要があります。

コードの書き方を説明する前に、スリープ機能についてもう少し説明します。プロセスをミリ秒単位で遅延させます。したがって、1秒は1000ミリ秒に相当し、10秒間一時停止する場合は、10000ミリ秒にする必要があります。

このVBAスリープExcelテンプレートはここからダウンロードできます–VBAスリープExcelテンプレート

例1

Subプロシージャの開始前にAPIコードを貼り付けたら、マクロ名を作成します。

コード:

#Sub Sleep_Example1()End Sub 

2つの変数を文字列として宣言します。

コード:

 Dim StartTime As String Dim EndTime As String 

以下のためのstartTime変数TIME関数の値を割り当てます。注: Excel関数のTIMEは、現在の時刻を返します。

コード:

StartTime =時間

次に、このメッセージをメッセージボックスVBAに表示します。

コード:

StartTime = Time MsgBox StartTime

次に、スリープ機能を使用してコードを10秒間一時停止します。私が言ったように、コードはミリ秒単位で一時停止するので、10秒間一時停止するには、10000ミリ秒を使用する必要があります。

コード:

 Sub Sleep_Example1()Dim StartTime As String Dim EndTime As String StartTime = Time MsgBox StartTime Sleep(10000)End Sub 

次に、2番目の変数EndTimeを使用して、現在の時刻を割り当てます。

コード:

 Sub Sleep_Example1() Dim StartTime As String Dim EndTime As String StartTime = Time MsgBox StartTime Sleep (10000) EndTime = Time MsgBox EndTime End Sub 

Now two variables StartTime and EndTime will hold macro beginning time and ending time. Run this macro, at first we will see macro starting time i.e. current time in your system.

Click on OK, it will sleep for 10 seconds. You can see the buffer symbol.

After 10 seconds it will start to resume the code so, it will show the end time i.e. after waiting for 10 seconds what’s the current time now.

Now you can see macro started at 10:54:14 and ended at 10:54:24 i.e. exactly the 10-second difference is there. In those 10 seconds, VBA pause the code running.

Example #2 – Sleep Function in Loops

Sleep is best used with loops in VBA. For example, I want to insert serial numbers from 1 to 10 using Do while loop in VBA.

After inserting the one number my code should wait for 3 seconds, so when the loop runs for 10 times it should be 30 seconds in total.

Code:

 Sub Sleep_Example2() Dim k As Integer k = 1 Do While k <= 10 Cells(k, 1).Value = k k = k + 1 Sleep (3000) '1000 milliseconds is 1 second so 3000 is equal to 3 seconds Loop End Sub 

Run this code and you have to wait for a minimum of 30 seconds to complete the process.

To track the exact time use the below code.

Code:

 Sub Sleep_Example2() Dim k As Integer Dim StartTime As String Dim EndTime As String StartTime = Time MsgBox "Your Code Started at " & StartTime k = 1 Do While k <= 10 Cells(k, 1).Value = k k = k + 1 Sleep (3000) '1000 milliseonds is 1 second so 3000 is equal to 3 seconds Loop EndTime = Time MsgBox "Your Code Ended at " & EndTime End Sub 

This code will display you 2 message box, the first one will show the starting time and the second one will show the end time.

Note: While running this code, you cannot use excel, even the escape key will not work.