VBA for Access 缺乏简单的Max(x,y)
函数查找两个或多个值的数学最大值。我习惯于在来自其他语言(例如 perl/php/ruby/python 等)的基本 API 中已经拥有这样的函数。
我知道可以做到: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 [email protected] /cdn-cgi/l/email-protection
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 [email protected] /cdn-cgi/l/email-protection
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
至于为什么 Access 不实现它,在我看来,这并不是一个很常见的事情。它也不是非常“数据库”。您已经获得了跨域和行集查找最大值/最小值所需的所有函数。实现起来也不是很难,或者在需要时只编写代码作为一次性比较。
也许以上内容会对某人有所帮助。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)