CPU に制御を移す(DoEvents 関数)
DoEvents 関数
DoEvents 関数を実行すると、CPU に制御を移し、他のアプリケーションが実行できるようになります。
DoEvents
DoEvents 関数 使用例
ユーザーフォーム
ユーザーフォームを挿入し(UserForm1)、ラベル(Label1)1つとコマンドボタン(CommandButton1、CommandButton2)2つを追加します。
サンプルVBAソース
1 2 3 4 5 6 7 |
Sub Sample_DoEvents() UserForm1.Show End Sub |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 |
Private i As Long Private myTimer As Boolean '「START」ボタンをクリックした時の処理 Private Sub CommandButton1_Click() myTimer = True Call Sample_Timer End Sub '「STOP」ボタンをクリックした時の処理 Private Sub CommandButton2_Click() myTimer = False End Sub 'UserForm1 を表示した時の処理 Private Sub UserForm_Initialize() 'Label1 に「0 回」を表示 UserForm1.Label1.Caption = "0 回" 'CommandButton1 に「START」を表示 UserForm1.CommandButton1.Caption = "START" 'CommandButton2 に「STOP」を表示 UserForm1.CommandButton2.Caption = "STOP" End Sub 'Label1 に 回数を表示します Sub Sample_Timer() Do While myTimer = True i = i + 1 Label1.Caption = i & " 回" DoEvents Loop End Sub |
実行結果
「標準モジュール」の Sample_DoEvents を実行
「START」(CommandButton1)をクリック、その後「STOP」(CommandButton2)をクリック
上記のサンプルで、DoEvents は、必ず指定してください。外すと無限ループに入ってしまうので、注意してください。
関連記事
-
-
配列の要素数を取得する(UBound 関数・LBound 関数)
UBound 関数・LBound 関数 【書式】 result = UBound …
-
-
VBAの変数宣言
VBAの変数宣言について 変数を暗黙的に宣言する エクセル VBA の場合、明示 …
-
-
指定した値を8進数で表記した文字列に変換する(Oct 関数)
Oct 関数 の使い方 【書式】 result = Oct ( expressi …
-
-
文字列:文字列右から指定文字数取得(Right 関数,RightB 関数)
Right 関数・RightB 関数 の使い方 【書式】 result_righ …
-
-
Erase ステートメント(配列の初期化・動的配列のメモリ解放)
Erase ステートメントの使い方 【書式】 Erase myArray [, …
-
-
Math 関数( Abs,Atn,Cos,Exp,Log,Rnd,Sgn,Sin,Sqr,Tan )
Abs 関数 【書式】 value = Abs ( number ) 【引数・戻 …
-
-
VBA の演算子(文字連結演算子:& 演算子 + 演算子)
文字連結演算子 文字連結演算子は、文字列の連結を行うときに使用する演算子で、「& …
-
-
指定した値を16進数で表記した文字列に変換する(Hex 関数)
Hex 関数 の使い方 【書式】 result = Hex ( expressi …
-
-
変数がオブジェクト型かどうかをチェックする( IsObject 関数 )
IsObject 関数の使い方 【書式】 result = IsObject ( …
-
-
Join 関数(配列の要素を結合)
Join 関数 1次元配列の各要素を指定した区切り文字で連結します。 【書式】 …