Excel でステータス ダイアログ ボックスを作成するにはどうすればよいですか
質問
Excelでデータベースレポートジェネレーターを作成しました。プログラムの実行時にステータス情報を表示するダイアログ ボックスを作成しようとしています。
レポートを生成すると、ダイアログ ボックスが表示されますが、表示される情報を更新/更新できません。ほとんどの場合、ダイアログ ボックスは部分的にのみ表示されます。.repaint メソッドを使用してみましたが、やはり同じ結果が得られます。レポートが生成された後、完全なダイアログ ボックスのみが表示されます。
解決
以下のコードは、Excel (XP 以降) 内でアクションを実行する場合に適切に機能します。
Excel の外部で行われるアクション (データベースへの接続やデータの取得など) の場合、これが提供する最善の方法は、アクションの前後にダイアログを表示する機会です (例: 「データの取得」, 「データを取得しました」)
というフォームを作成します 「frmステータス」, というラベルをフォームに貼り付けます。 「ラベル1」.
フォームのプロパティを設定する 'ShowModal' = false, 、これにより、フォームが表示されている間にコードを実行できるようになります。
Sub ShowForm_DoSomething()
Load frmStatus
frmStatus.Label1.Caption = "Starting"
frmStatus.Show
frmStatus.Repaint
'Load the form and set text
frmStatus.Label1.Caption = "Doing something"
frmStatus.Repaint
'code here to perform an action
frmStatus.Label1.Caption = "Doing something else"
frmStatus.Repaint
'code here to perform an action
frmStatus.Label1.Caption = "Finished"
frmStatus.Repaint
Application.Wait (Now + TimeValue("0:00:01"))
frmStatus.Hide
Unload frmStatus
'hide and unload the form
End Sub
他のヒント
を追加してみてください ドゥイベント ループ内で呼び出します。これにより、フォームを再描画して他のリクエストを受け入れることができるようになります。
以前に開発した同様のアプリケーションの進行状況情報を表示するために、Excel 独自のステータス バー (ウィンドウの左下) を使用しました。
これは、進行状況に関するテキスト更新を表示したいだけの場合に非常にうまく機能し、更新ダイアログの必要性をまったく回避します。
オーケー、@JonnyGold、これが私が使用したものの例です...
Sub StatusBarExample()
Application.ScreenUpdating = False
' turns off screen updating
Application.DisplayStatusBar = True
' makes sure that the statusbar is visible
Application.StatusBar = "Please wait while performing task 1..."
' add some code for task 1 that replaces the next sentence
Application.Wait Now + TimeValue("00:00:02")
Application.StatusBar = "Please wait while performing task 2..."
' add some code for task 2 that replaces the next sentence
Application.Wait Now + TimeValue("00:00:02")
Application.StatusBar = False
' gives control of the statusbar back to the programme
End Sub
お役に立てれば!
ワークブックに空白のシートを挿入します。"情報"
Sheets("information").Select
Range("C3").Select
ActiveCell.FormulaR1C1 = "Updating Records"
Application.ScreenUpdating = False
Application.Wait Now + TimeValue("00:00:02")
マクロを続行
Sheets("information").Select
Range("C3").Select
Application.ScreenUpdating = True
ActiveCell.FormulaR1C1 = "Preparing Information"
Application.ScreenUpdating = False
Application.Wait Now + TimeValue("00:00:02")
マクロを続行
など、新しいシートを挿入する代わりに、既存のシートのどこかに空白のセルを選択します
Range("C3").Select
ActiveCell.FormulaR1C1 = "Updating Records"
Application.ScreenUpdating = False
Application.Wait Now + TimeValue("00:00:02")
等
ダイアログ ボックスも同じ UI スレッドで実行されます。なので、自分自身を再描画するのに忙しすぎます。VBA に優れたマルチスレッド機能があるかどうかはわかりません。