DGM*_*DGM 13 ms-access vba max worksheet-function access-vba
VBA for Access缺少一个简单的Max(x,y)函数来查找两个或更多值的数学最大值.我习惯于在基础API中已经有来自其他语言的这样的函数,例如perl/php/ruby/python等.
我知道可以做到:IIf(x > y, x,y).还有其他解决方案吗?
我会把这个问题解释为:
如何在Access中实现一个返回数字数组的最大值/最小值的函数?这是我使用的代码(通过与IIf类比命名为"iMax",即"Immediate If"/"Immediate Max"):
Public Function iMax(ParamArray p()) As Variant
' Idea from Trevor Best in Usenet MessageID rib5dv45ko62adf2v0d1cot4kiu5t8mbdp@4ax.com
Dim i As Long
Dim v As Variant
v = p(LBound(p))
For i = LBound(p) + 1 To UBound(p)
If v < p(i) Then
v = p(i)
End If
Next
iMax = v
End Function
Public Function iMin(ParamArray p()) As Variant
' Idea from Trevor Best in Usenet MessageID rib5dv45ko62adf2v0d1cot4kiu5t8mbdp@4ax.com
Dim i As Long
Dim v As Variant
v = p(LBound(p))
For i = LBound(p) + 1 To UBound(p)
If v > p(i) Then
v = p(i)
End If
Next
iMin = v
End Function
Run Code Online (Sandbox Code Playgroud)
至于为什么Access不会实现它,在我看来,它不是一个非常普遍的需要.它也不是非常"数据集".您已经拥有了跨域和行集查找Max/Min所需的所有功能.它也不是很难实现,或者只是在需要时进行一次性比较.
也许上面会帮助别人.