Solved

Divide by zero

Posted on 2006-10-25
3
335 Views
Last Modified: 2010-04-30
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?
0
Comment
Question by:isnoend2001
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 65 total points
ID: 17807907
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
0
 
LVL 19

Assisted Solution

by:BrianGEFF719
BrianGEFF719 earned 60 total points
ID: 17808143
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
0
 

Author Comment

by:isnoend2001
ID: 17808621
Thanks guys
BrianGEFF719
Your code initially works but R,5 does not get updated if R,3 is erased
0

Featured Post

Independent Software Vendors: We Want Your Opinion

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

If you have ever used Microsoft Word then you know that it has a good spell checker and it may have occurred to you that the ability to check spelling might be a nice piece of functionality to add to certain applications of yours. Well the code that…
Article by: Martin
Here are a few simple, working, games that you can use as-is or as the basis for your own games. Tic-Tac-Toe This is one of the simplest of all games.   The game allows for a choice of who goes first and keeps track of the number of wins for…
Get people started with the process of using Access VBA to control Outlook using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Microsoft Outlook. Using automation, an Access applic…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…

679 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question