This course will introduce you to C++ 11 and teach you about syntax fundamentals.

Can anyone tell me why the following code does not work?

Sub Test()

Dim x As Single

x = 300 / 100000

If 300 Mod x = 0 Then Stop

End Sub

x is slightly larger than zero, but VBA says the 300 Mod x will not work because I'm dividing by zero.

Thanks for any help

JRA in Priddis, Alberta

Sub Test()

Dim x As Single

x = 300 / 100000

If 300 Mod x = 0 Then Stop

End Sub

x is slightly larger than zero, but VBA says the 300 Mod x will not work because I'm dividing by zero.

Thanks for any help

JRA in Priddis, Alberta

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

--JRA

Sub Test()

Dim x As Double

Dim y As Double

y = 300

x = y / 100000

If y Mod x = 0 Then Stop

End Sub

Result: Division by zero error.

It goes without saying that using "Single" instead of "Double" does not work either.

I suspect there is some work-around, but I cannot think of anything else to try.

--JRA in Priddis, Alberta

Anyway, the problem in my code is that x is a very small number created by dividing y by 100000. If we move that 100000 to the left side of the equation and have it multiply x, then we avoid any small number problem with MOD. Here is my revised version:

Sub Test2()

Dim x As Integer

Dim y As Long

x = 300

y = 100000

If x * y Mod x = 0 Then Stop

End Sub

That solves my problem, but I'll leave the question open for a couple of days to see if anyone can think of a better solution.

Thanks to Chaau for his suggested solution. I suspect that it would work in Visual Basic, but I'm using VBA.

--JRA in Priddis

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trialAnyway, the problem in my code is that x is a very small number created by dividing y by 100000. If we move that 100000 to the left side of the equation and have it multiply x, then we avoid any small number problem with MOD. Here is my revised version:

Sub Test2()

Dim x As Integer

Dim y As Long

x = 300

y = 100000

If x * y Mod x = 0 Then Stop

End Sub

That solves my problem, but I'll leave the question open for a couple of days to see if anyone can think of a better solution.

Thanks to Chaau for his suggested solution. I suspect that it would work in Visual Basic, but I'm using VBA.

--JRA in Priddis

(rounding floating-point numbers to integers)This means that Mod operator for VBA is indeed different to the VB version. The VBA version works only with the whole numbers, but VB works with floating point numbers as well

The solution is to divide both sides of the equation by some figure before the computer sees “x * y”. In the revised code below, I have divided both sides around the equals sign by 10:

Sub Test2()

Dim x As Integer

Dim y As Long

x = 300

y = 100000

If x * (y / 10) Mod (x / 10) = 0 Then Stop

End Sub

Apologies for my error.

--JRA

The work-around-solution I suggest solves the problem, but all the points should go to Chaau.

Thanks!

JRA in Priddis

Regular Expressions

From novice to tech pro — start learning today.

Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

Experts Exchange Solution brought to you by

Enjoy your complimentary solution view.

Get every solution instantly with premium.
Start your 7-day free trial.

Open in new window

Otherwise VBA thinks that Mod operation should be performed on Integers and converts the x variable to Integer.Read here about the Single data type