此功能MRound
在MS Access中不存在,您不能只安装它。
我正在尝试在Access中使用mround
功能报告。该函数未在表达式中列出内置函数的构建器框。当我使用计算,例如=mround([AmountDue],0.05)
,以及运行报告,它要求输入参数mround
。
您可以在公共VBA模块中定义自己的模块,例如:
Function MRound(dblNum As Double, dblMtp As Double) As Double
MRound = dblMtp * Round(dblNum / dblMtp, 0)
End Function
或者,将对Microsoft Excel对象库的引用添加到您的VBA项目(工具>引用),然后在公共VBA模块中将MRound
函数定义为:
Function MRound(dblNum As Double, dblMtp As Double) As Double
MRound = Excel.WorksheetFunction.MRound(dblNum, dblMtp)
End Function
Access中没有MRound
,因此请创建您自己的舍入函数。
但是,切勿为此使用Round
,因为它臭名昭著的越野车。因此,请使用普通数学和数据类型Decimal以避免错误:
' Rounds a value by 4/5 to the nearest multiplum of a rounding value.
' Accepts any value within the range of data type Currency.
' Mimics Excel function MRound without the limitations of this.
'
' Examples:
'
' RoundAmount(-922337203685477.5808, 0.05) -> -922337203685477.6
' RoundAmount( 922337203685477.5807, 0.05) -> 922337203685477.6
' RoundAmount( 10, 3) -> 9
' RoundAmount(-10,-3) -> -9
' RoundAmount( 1.3, 0.2) -> 1.4
' RoundAmount( 122.25, 0.5) -> 122.5
' RoundAmount( 6.05, 0.1) -> 6.1
' RoundAmount( 7.05, 0.1) -> 7.1
' 2009-05-17. Gustav Brock, Cactus Data ApS, CPH.
'
Public Function RoundAmount( _
ByVal Value As Currency, _
ByVal RoundValue As Currency) _
As Variant
Dim BaseValue As Variant
Dim Result As Variant
BaseValue = Int(Value / CDec(RoundValue) + CDec(0.5))
Result = BaseValue * RoundValue
RoundAmount = Result
End Function
例如,此函数将正确舍入:
Amount = RoundAmount( 122.25, 0.5)
Amount -> 122.50
有关精度舍入的更多信息,请参考我在GitHub上的项目:
和本文中提及的文章。