VBA相当于Excel的mod函数

发布于 2024-10-06 07:38:18 字数 42 浏览 6 评论 0 原文

有没有相当于excel的mod函数的vba?

Is there a vba equivalent to excel's mod function?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(9

木格 2024-10-13 07:38:18

在vba中,该函数是MOD。
例如,

 5 MOD 2

这是一个有用的链接

In vba the function is MOD.
e.g

 5 MOD 2

Here is a useful link.

夜唯美灬不弃 2024-10-13 07:38:18

您需要 mod 运算符

The expression a Mod b is equivalent to the following formula:

a - (b * (a \ b))

编辑添加:

您可能需要考虑一些特殊情况,因为 Excel 使用浮点数学(并返回 float),而 VBA 函数返回整数。因此,对浮点数使用 mod 可能需要额外注意:

  • Excel 的结果可能与您的预测不完全一致;对此进行了简要介绍此处(参见最上面的答案) 以及详细内容 此处

  • 正如 @André 在评论中指出的那样,负数可能会与您期望的方向相反。他建议的 Fix() 函数已解释此处 (MSDN)

You want the mod operator.

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. Because of this, using mod with floating-point numbers may require extra attention:

  • Excel's results may not correspond exactly with what you would predict; 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. The Fix() function he suggests is explained here (MSDN).

迷路的信 2024-10-13 07:38:18

我在 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

在 VBA 模块中

My way to replicate Excel's MOD(a,b) in VBA is to use XLMod(a,b) in VBA where you include the function:

Function XLMod(a, b)
    ' This replicates the Excel MOD function
    XLMod = a - b * Int(a / b)
End Function

in your VBA Module

茶花眉 2024-10-13 07:38:18

使用 Excel MOD(a,b) 函数和 VBA a Mod b 运算符时要非常小心。
Excel 返回浮点结果,VBA 返回整数。

在 Excel 中 =Mod(90.123,90) 返回 0.123000000000005 而不是 0.123
在 VBA 中 90.123 Mod 90 返回 0

它们当然不等价!

等效项有:
在 Excel 中: =Round(Mod(90.123,90),3) 返回 0.123 且
在 VBA 中:((90.123 * 1000) Mod 90000)/1000 也返回 0.123

Be very careful with the Excel MOD(a,b) function and the VBA a Mod b operator.
Excel returns a floating point result and VBA an integer.

In Excel =Mod(90.123,90) returns 0.123000000000005 instead of 0.123
In VBA 90.123 Mod 90 returns 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

︶ ̄淡然 2024-10-13 07:38:18

Mod 运算符大致相当于MOD 函数:

number Mod divisor 大致相当于 MOD(number, divisor)

The Mod operator, is roughly equivalent to the MOD function:

number Mod divisor is roughly equivalent to MOD(number, divisor).

海螺姑娘 2024-10-13 07:38:18
Function Remainder(Dividend As Variant, Divisor As Variant) As Variant
    Remainder = Dividend - Divisor * Int(Dividend / Divisor)
End Function

此函数始终有效,并且是 Excel 函数的精确副本。

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.

[浮城] 2024-10-13 07:38:18

但如果你只是想区分奇数迭代和偶数迭代之间的区别,这很好用:

If i Mod 2 > 0 then 'this is an odd 
   'Do Something
Else 'it is even
   'Do Something Else
End If

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
耶耶耶 2024-10-13 07:38:18

我发现如果 excel 的 mod 函数也给出不正确的结果,这是最可靠的:

最初甚至
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

I've found this to be the most reliable if excel's mod function is also giving incorrect results:

Originally even
XLMod = (a - (b * Int(a / b))) was giving me incorrect results so..

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
云柯 2024-10-13 07:38:18

楼上的答案其实是错误的。

建议的方程:
a - (b * (a \ b))

将求解: a - a

在所有情况下当然都是 0。

正确的等式是:

a - (b * INT(a \ b))

或者,如果数字 (a) 可以为负数,请使用:

a - (b * FIX(a \ b))

The top answer is actually wrong.

The suggested equation:
a - (b * (a \ b))

Will solve to: a - a

Which is of course 0 in all cases.

The correct equation is:

a - (b * INT(a \ b))

Or, if the number (a) can be negative, use this:

a - (b * FIX(a \ b))

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文