次の VBA コードは、ニーズに合わせてカスタマイズできる単純なフレームワークを提供します。画面の更新をオフにしたり、比較をワークシートから配列に移動したりするなど、質問へのコメントで言及されている多くの最適化が組み込まれています。
マクロがかなり大規模な比較と置換を行うことに気付くでしょう。私が実行したデータセットは、範囲 A1:Y100000 の 1 から 1000 までの 250 万個の乱数でした。数値が 250 より大きく 500 未満の場合は、0 に置き換えました。これには、データ セット内のすべての数値の 24.9% を置き換える必要がありました。
Sub ReplaceExample()
Dim arr() As Variant
Dim rng As Range
Dim i As Long, _
j As Long
Dim floor as Long
Dim ceiling as Long
Dim replacement_value
'assign the worksheet range to a variable
Set rng = Worksheets("Sheet2").Range("A1:Y100000")
floor = 250
ceiling = 500
replacement_value = 0
' copy the values in the worksheet range to the array
arr = rng
' turn off time-consuming external operations
Application.ScreenUpdating = False
Application.Calculation = xlCalculationManual
Application.EnableEvents = False
'loop through each element in the array
For i = LBound(arr, 1) To UBound(arr, 1)
For j = LBound(arr, 2) To UBound(arr, 2)
'do the comparison of the value in an array element
'with the criteria for replacing the value
If arr(i, j) > floor And arr(i, j) < ceiling Then
arr(i, j) = replacement
End If
Next j
Next i
'copy array back to worksheet range
rng = arr
'turn events back on
Application.ScreenUpdating = True
Application.Calculation = xlCalculationAutomatic
Application.EnableEvents = True
End Sub
この単純な比較と置換をコーディングするためのさまざまな代替案について、いくつかのパフォーマンス テストを行いました。その結果は、他の人による VBA パフォーマンス結果と一致すると予想されます。各代替案を 10 回実行し、各実行の経過時間を計算し、10 回の経過時間を平均しました。

この結果は、配列を使用すると、特にデータ セットが大きい場合に大きな影響を与える可能性があることを示しています。この場合、平均実行時間が 3.6 分から 4 秒に 98% 短縮されました。
外部イベントをオフにする最適化により、ワークシート操作に顕著な違いが生じ、実行時間が 22% 短縮されましたが、ほとんどの計算作業が配列ベースである場合、これらの最適化はほとんど影響を与えませんでした。