Link to home
Start Free TrialLog in
Avatar of JohnRobinAllen
JohnRobinAllenFlag for Canada

asked on

VBA Division by zero

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
Avatar of chaau
chaau
Flag of Australia image

You need to append literal F to the number 300, like this:
If 300F Mod x = 0 Then Stop

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
Avatar of JohnRobinAllen

ASKER

I wish Chaau's comment would work, but when I tried it in the VBA code inside a Word document, it produced a message "Syntax Error" and highlighted the line with "300F".
     --JRA
I also tried this:
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
ASKER CERTIFIED SOLUTION
Avatar of JohnRobinAllen
JohnRobinAllen
Flag of Canada image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
There is an obvious work-around to my problem that I should have thought about before I submitted the problem to EE. The solution is not elegant since it does not address the problem that MOD seems to think that very small numbers are equivalent to zero.
     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
SOLUTION
Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial
Unfortunately I found a flaw in the solution I had proposed earlier. The calculation of x * y can produce an overflow that stops the program.
     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
Chaau's comment and link show why the problem occurred: in VBA "MOD" rounds decimal values to integers.
The work-around-solution I suggest solves the problem, but all the points should go to Chaau.
Thanks!
     JRA in Priddis