Application.ScreenUpdating プロパティ (Excel) | Microsoft Learn

この記事では、ExcelのVBAマクロでの画面更新の管理について説明しています。画面更新をオフにすることで、マクロの実行速度を向上させることができますが、その間は処理内容が見えなくなります。マクロの終了時には必ず画面更新を再びオンにする必要があります。具体的な例では、Sheet1の偶数列を非表示にする処理を行い、画面更新のオン・オフで所要時間を比較する手法が示されています。この方法により、処理の速度向上が確認できます。

Excel VBAにおけるScreenUpdatingプロパティ

Excel VBA(Visual Basic for Applications)を使用する際、マクロの実行速度を向上させるために「ScreenUpdating」プロパティを利用することができます。このプロパティは、画面の更新が行われるかどうかを制御するもので、マクロの実行中に画面の再描画を一時的に停止することが可能です。

ScreenUpdatingプロパティとは?

ScreenUpdating プロパティは、画面の更新が有効かどうかを示す Boolean(真偽値)型のプロパティです。画面更新がオンの場合、マクロの処理中に行われている変更がリアルタイムで表示されますが、これが原因で処理が遅くなることがあります。

使用方法

expression.ScreenUpdating

ここで、expressionApplication オブジェクトを表す変数です。

注意事項

画面の更新をオフにすることで、マクロの実行速度が向上しますが、実行中の処理を視覚的に確認することができないため、特に長時間かかる処理を行う場合などに便利です。処理が終了した際には必ず ScreenUpdating プロパティを True に戻すことを忘れないようにしましょう。

実例

以下の例では、Sheet1の偶数列を非表示にし、その処理時間を測定します。最初の実行では画面更新をオンにし、2回目の実行ではオフにします。これにより、実行時間の違いを比較できます。

Dim elapsedTime(2)
Application.ScreenUpdating = True ' 画面更新をオンにする
For i = 1 To 2
    If i = 2 Then Application.ScreenUpdating = False ' 2回目は画面更新をオフにする
    startTime = Time
    Worksheets("Sheet1").Activate
    For Each c In ActiveSheet.Columns
        If c.Column Mod 2 = 0 Then
            c.Hidden = True ' 偶数列を非表示にする
        End If
    Next c
    stopTime = Time
    elapsedTime(i) = (stopTime - startTime) * 24 * 60 * 60 ' 経過時間を計算
Next i
Application.ScreenUpdating = True ' 画面更新をオンに戻す
MsgBox "Elapsed time, screen updating on: " & elapsedTime(1) & _
       " sec." & Chr(13) & _
       "Elapsed time, screen updating off: " & elapsedTime(2) & _
       " sec."

ユーザーサポートとフィードバック

Office VBAやこのドキュメントに関する質問やフィードバックがある場合は、Office VBAサポートとフィードバックをご覧ください。サポートを受ける方法やフィードバックを提供する方法についてのガイダンスが記載されています。

このように、ScreenUpdating プロパティを上手に活用することで、Excel VBAの処理速度を向上させ、効率的なマクロの作成が可能となります。ぜひ実践してみてください。

————-

Application.ScreenUpdating property (Excel) | Microsoft Learn

Source link

The article discusses the ScreenUpdating property in VBA (Visual Basic for Applications), which controls whether the screen updates while a macro is running. Setting ScreenUpdating to False can significantly improve the performance of a macro by preventing the user interface from redrawing during execution, allowing the code to run faster, although the process will not be visible to the user until it completes.

The provided syntax for the ScreenUpdating property is expression.ScreenUpdating, where expression is a variable representing an Application object. The article emphasizes the importance of resetting this property to True after the macro execution to ensure the screen updates correctly afterward.

An example is included to illustrate the difference in execution time when screen updating is toggled on and off. In this example, every other column on "Sheet1" is hidden twice: first with screen updating enabled, and then with it disabled. The execution times for both scenarios are measured and displayed in a message box, demonstrating that turning off screen updating can lead to faster execution.

Lastly, there’s a note inviting readers to seek help and provide feedback regarding Office VBA documentation.

関連記事