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 は、必ず指定してください。外すと無限ループに入ってしまうので、注意してください。
関連記事
-
-
ショートカットメニュー(CommandBar オブジェクト)
ショートカットメニュー(コンテキストメニュー) 右クリックで表示されるメニューの …
-
-
日付時刻:時・分・秒を取得(Hour、Minute、Second 関数)
日付時刻から時・分・秒を取得する関数 【書式】 time_hour = Hour …
-
-
日付時刻:日付時刻から情報取得( DatePart 関数 )
DatePart 関数(日付・時刻の値から情報を取得) 【書式】 result …
-
-
カレントフォルダの取得・設定(DefaultFilePath プロパティ)
DefaultFilePath プロパティ カレントフォルダとは、ブックを開いた …
-
-
文字列:文字列の指定した位置から指定文字数取得(Mid 関数,MidB 関数)
Mid 関数・MidB 関数 の使い方 【書式】 result_mid = Mi …
-
-
指定した数値に対応する選択肢の値・数式を返す(Choose 関数)
Choose 関数 の使い方 Choose 関数は、指定されたインデックス値(1 …
-
-
ある数値が、複数の範囲のどの範囲に含まれるかを示す文字列を返す(Partition 関数)
Partition 関数 の使い方 Partition 関数は、ある数値が、区切 …
-
-
VBA の演算子(比較演算子)
比較演算子 比較演算子による演算の結果は「真」の場合は「True」、「偽」の場合 …
-
-
式の値に応じて多分岐を行う(Switch 関数)
Switch 関数 の使い方 Switch 関数は、「式・値」を対にして指定して …
-
-
文字列:文字列を最後から検索してその最初の文字位置を返す(InStrRev 関数)
InStrRev 関数 任意の文字列の中で、指定した文字列を最後から検索して、見 …


