Excel でデータベース レポート ジェネレーターを作成しました。プログラムの実行時にステータス情報を表示するダイアログ ボックスを作成しようとしています。
レポートを生成すると、ダイアログ ボックスは表示されますが、表示される情報を更新/更新できません。ほとんどの場合、ダイアログ ボックスは部分的にしか表示されません。.repaint メソッドを使用してみましたが、それでも同じ結果が得られます。レポートが生成された後にのみ、完全なダイアログ ボックスが表示されます。
過去に開発した同様のアプリケーションの進捗情報を表示するために、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
お役に立てれば!
ループにDoEvents呼び出しを追加してみてください。これにより、フォームを再描画して他のリクエストを受け入れることができます。
以下のコードは、Excel(XP以降)内でアクションを実行するときに適切に機能します。
Excelの外部で実行されるアクションの場合、たとえばデータベースに接続してデータを取得する場合、これが提供する最善の方法は、アクションの前後にダイアログを表示する機会です(たとえば、「データの取得」、「データの取得」) 。
「frmStatus」というフォームを作成し、「Label1」というフォームにラベルを付けます。
フォームプロパティ'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
ワークブックに空白のシートを挿入します シートの名前を変更します。"情報"
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 に優れたマルチスレッド機能があるかどうかはわかりません。