We help IT Professionals succeed at work.

# Divide by zero

on
Medium Priority
350 Views
Hi Experts
I am using an editable Mshflexgrid and want to store the results of dividing one cell by another in another cell.
Instead of checking the contents for 0 or "" just update R,5 and  exit sub if 0 or"" using On Error
R = Grid.Row
C= Grid.Col
On Error GoTo ExitSub

If C < 3 Or C > 4 Then Exit Sub

With Grid

.TextMatrix(R, 5) = Val(.TextMatrix(R, 4)) / Val(.TextMatrix(R, 3))
If Val(.TextMatrix(R, 5)) <> 0 Then
.TextMatrix(R, 5) = Format(.TextMatrix(R, 5), "Fixed")
End If

End With
Exit Sub

ExitSub:
Grid.TextMatrix(R, 5) = ""

End Sub
Is there something with writing this way?
Comment
Watch Question

## View Solutions Only

Retired
CERTIFIED EXPERT
Top Expert 2012
Commented:
It is slightly bad practise to use errors functionally, but many do. Personally I would check the divisor value.

If you don't check which error you are getting, you could be covering up another error. It's also good practice to reset the error with a Resume statement

On Error GoTo ErrorSub
'...

ExitSub:
Exit Sub
ErrorSUB:
If Err.Number = 11 then
Grid.TextMatrix(R, 5) = ""
Else
MsgBox "Error in procedure: MyProcedure. " & Err.Number & ", " & Err.description"
Endif
Resume ExitSub
End Sub

Not the solution you were looking for? Getting a personalized solution is easy.

Commented:
I would say that you should just do:

With Grid

if not (Val(.TextMatrix(R, 3)) = 0)
.TextMatrix(R, 5) = Val(.TextMatrix(R, 4)) / Val(.TextMatrix(R, 3))
If Val(.TextMatrix(R, 5)) <> 0 Then 'only if TextMatrix(R,4) = 0.
.TextMatrix(R, 5) = Format(.TextMatrix(R, 5), "Fixed")
End If
else
Grid.TextMatrix(R, 5) = "" 'case of division by zero
End If

end with

Commented:
Thanks guys
BrianGEFF719
Your code initially works but R,5 does not get updated if R,3 is erased
##### Thanks for using Experts Exchange.

• View three pieces of content (articles, solutions, posts, and videos)
• Ask the experts questions (counted toward content limit)
• Customize your dashboard and profile