[英]VBA equivalent to Excel's mod function
Is there a vba equivalent to excel's mod
function?是否有一个 vba 相当于 excel 的
mod
function?
You want the mod operator . 你想要mod运算符 。
The expression a Mod b is equivalent to the following formula:
a - (b * (a \ b))
Edited to add: 编辑添加:
There are some special cases you may have to consider, because Excel is using floating point math (and returns a float
), which the VBA function returns an integer. 您可能需要考虑一些特殊情况,因为Excel使用浮点数学(并返回一个
float
),VBA函数返回一个整数。 Because of this, using mod
with floating-point numbers may require extra attention: 因此,使用带有浮点数的
mod
可能需要额外注意:
Excel's results may not correspond exactly with what you would predict; Excel的结果可能与您预测的结果不完全一致; this is covered briefly here (see topmost answer) and at great length here .
这里简要介绍一下(参见最顶层的答案)并在这里详细介绍 。
As @André points out in the comments, negative numbers may round in the opposite direction from what you expect. 正如@André在评论中指出的那样,负数可能会与您的预期相反。 The
Fix()
function he suggests is explained here (MSDN) . 他建议的
Fix()
函数在这里解释(MSDN) 。
My way to replicate Excel's MOD(a,b)
in VBA is to use XLMod(a,b)
in VBA where you include the function: 我在VBA中复制Excel的
MOD(a,b)
在VBA中使用XLMod(a,b)
,其中包含函数:
Function XLMod(a, b)
' This replicates the Excel MOD function
XLMod = a - b * Int(a / b)
End Function
in your VBA Module 在您的VBA模块中
Be very careful with the Excel MOD(a,b) function and the VBA a Mod b operator. 使用Excel MOD(a,b)函数和VBA和Mod b运算符时要非常小心。 Excel returns a floating point result and VBA an integer.
Excel返回浮点结果,VBA返回整数。
In Excel =Mod(90.123,90) returns 0.123000000000005 instead of 0.123 In VBA 90.123 Mod 90 returns 0 在Excel = Mod(90.123,90)中返回0.123000000000005而不是0.123在VBA中90.123 Mod 90返回0
They are certainly not equivalent! 他们当然不等同!
Equivalent are: In Excel: =Round(Mod(90.123,90),3) returning 0.123 and In VBA: ((90.123 * 1000) Mod 90000)/1000 returning also 0.123 相当于:在Excel中:= Round(Mod(90.123,90),3)返回0.123和In VBA :((90.123 * 1000)Mod 90000)/ 1000返回也是0.123
The Mod
operator , is roughly equivalent to the MOD
function : Mod
运算符大致相当于MOD
函数 :
number Mod divisor
is roughly equivalent to MOD(number, divisor)
. number Mod divisor
大致相当于MOD(number, divisor)
。
The top answer is actually wrong. 最佳答案实际上是错误的。
The suggested equation: a - (b * (a \\ b))
建议的等式:
a - (b * (a \\ b))
Will solve to: a - a
将解决:
a - a
Which is of course 0 in all cases. 在所有情况下,当然是0。
The correct equation is: 正确的等式是:
a - (b * INT(a \\ b))
Or, if the number (a) can be negative, use this: 或者,如果数字(a)可能是负数,请使用:
a - (b * FIX(a \\ b))
But if you just want to tell the difference between an odd iteration and an even iteration, this works just fine: 但是,如果您只是想区分奇数迭代和偶数迭代之间的区别,这可以正常工作:
If i Mod 2 > 0 then 'this is an odd
'Do Something
Else 'it is even
'Do Something Else
End If
Function Remainder(Dividend As Variant, Divisor As Variant) As Variant
Remainder = Dividend - Divisor * Int(Dividend / Divisor)
End Function
This function always works and is the exact copy of the Excel function. 此功能始终有效,并且是Excel功能的精确副本。
Originally even XLMod = (a - (b * Int(a / b))) was giving me incorrect results so..最初甚至 XLMod = (a - (b * Int(a / b))) 也给我不正确的结果所以..
Function XLMod(a, b As Long)
' This replicates the Excel MOD function - the VBA mod function returns an integer
Dim templong As Long
templong = (b * Int(a / b))
XLMod = (a - templong)
End Function
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.