VBA 相当于 Excel 的 mod 函数 [英] VBA equivalent to Excel's mod function
问题描述
是否有与 excel 的 mod
函数等效的 vba?
Is there a vba equivalent to excel's mod
function?
推荐答案
您想要 mod 运算符.
The expression a Mod b is equivalent to the following formula:
a - (b * (a b))
编辑添加:
您可能需要考虑一些特殊情况,因为 Excel 使用浮点数学(并返回 float
),而 VBA 函数返回一个整数.因此,将 mod
与浮点数一起使用可能需要特别注意:
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. Because of this, using mod
with floating-point numbers may require extra attention:
Excel 的结果可能与您的预测不完全一致;这是简要介绍 这里(见最上面的答案) 并且很长这里.
正如@André 在评论中指出的那样,负数可能与您预期的方向相反.他建议的 Fix()
函数解释了 这里 (MSDN).
As @André points out in the comments, negative numbers may round in the opposite direction from what you expect. The Fix()
function he suggests is explained here (MSDN).
这篇关于VBA 相当于 Excel 的 mod 函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!