My questions is somewhat related to this post .
However, let's say I want to use Excel's built-in MOD
(modulo) function in VBA. This site suggests that using Application.WorksheetFunction.[insert name of Excel function]
would provide the desired effect.
However, Application.WorksheetFunction.MOD
does not work.
In fact, when I simply type Application.WorksheetFunction.
into VBA, a dropdown menu appears providing a list of function names to choose from, but MOD
is not provided in this list!
2 questions:
MOD
function in VBA? Note: I started down this path b/c I'm trying to get 1.7 Mod 0.5
to equal 0.2
using VBA's Mod
function, but it produces #VALUE!
in the resulting Excel cell I apply my VBA function to. However, if I type MOD(1.7,0.5)
directly into Excel, I get the correct answer (ie, 0.2
)
You use as is since it is an operator (with integers). Use evaluate
if you want to use doubles etc eg
Eg
Debug.Print 2 Mod 12
Debug.Print Evaluate("Mod(1.7,0.5)")
Dim x As Double, y As Double
x = 1.7
y = 0.5
Debug.Print Evaluate("Mod(" & x & "," & y & ")")
See documentation . There are limitations with large numbers. A common workaround, from Microsoft, is:
=number-(INT(number/divisor)*divisor)
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.