値の範囲を入力 (列のみ) といくつかのしきい値として受け取る関数があります。しきい値を超える元の範囲のすべての値を含むようにフィルター処理された範囲を返したいと思います。次のコードがあります。
Public Function FilterGreaterThan(Rng As Range, Limit As Double) As Range
Dim Cell As Range
Dim ResultRange As Range
For Each Cell In Rng
If Abs(Cell.Value) >= Limit Then
If ResultRange Is Nothing Then
Set ResultRange = Cell
Else
Set ResultRange = Union(ResultRange, Cell)
End If
End If
Next
Set FilterGreaterThan = ResultRange
End Function
問題は、数値がしきい値を下回ると、その後のしきい値を超える他の数値が範囲に追加されないことです。
例えば:
Threshold - 2
Numbers -
3
4
1
5
3 と 4 を追加してループしますが、5 は追加されません。#value エラーが発生します。しかし、エラーは発生せず、範囲 - 3, 4 または範囲 - 3, 4, 1 のみを入力すると正常に動作します。