3

関数をセルの範囲で実行したいのですが、次の場合:

  • いずれかがNormalValue「低すぎる」を返します。

  • NormalValue範囲内の最大値の 2 倍より大きい場合は、「高すぎます」を返します。

  • どちらも真でない場合は、'OK' を返します。

これは私がこれまでに思いついたものです:

Function TooHighLow(rng As range, NormalValue As Double)

  For Each cell In rng
     If Application.WorksheetFunction.Max(cell.Value) > NormalValue Then
        TooHighLow = "Too Low"

     ElseIf NormalValue > 2 * (Application.WorksheetFunction.Max(cell.Value)) Then
        TooHighLow = "Too High"

     Else
        TooHighLow = "OK"

     End If
  Next cell
End Function 
4

3 に答える 3

0

セルの範囲から単一の安値または高値を見つけようとしている場合は、未処理の値を受け入れて、その時点で関数を終了する必要があります。ループを続行すると、範囲内の次のセルが評価されるもので未処理の値が上書きされます。

Function TooHighLow(rng As range, NormalValue As Double)
  dim cell as range
  'start with a default value
  TooHighLow = "OK"
  For Each cell In rng
     If Application.WorksheetFunction.Max(cell.Value) > NormalValue Then
        'set the function to return Too Low
        TooHighLow = "Too Low"
        'exit the For Next loop
        Exit For
     ElseIf NormalValue > 2 * (Application.WorksheetFunction.Max(cell.Value)) Then
        'set the function to return Too High
        TooHighLow = "Too High"
        'exit the For Next loop
        Exit For
     End If
     'if the loop has not been exited, the next cell in the range will be evaluated
     'if the loop has been exited, the function will return the outstanding value
  Next cell

End Function 
于 2015-09-22T11:30:44.453 に答える