我想要一个函数来运行一系列单元格,如果:
任何大于NormalValue
然后返回'太低',
NormalValue
大于范围内最大值的两倍
然后返回太高'
这是我到目前为止所提出的:
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
答案 0 :(得分:2)
我想你想要这样的东西:
Function TooHighLow(rng As Range, NormalValue As Double)
Dim m As Double
m = Application.WorksheetFunction.Max(rng)
If m > NormalValue Then
TooHighLow = "Too Low"
ElseIf NormalValue > 2 * m Then
TooHighLow = "Too High"
Else
TooHighLow = "OK"
End If
End Function
1)循环毫无意义
2)你应该只计算一次最大值,将结果存储在一个变量中。
答案 1 :(得分:1)
没有VBA:
=IF(MAX(range)>NormalValue,"too low",IF(NormalValue>2*MAX(range),"too high","OK"))
答案 2 :(得分: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