Solved

Divide by zero

Posted on 2006-10-25
3
331 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
Comment Utility
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
Comment Utility
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
Comment Utility
Thanks guys
BrianGEFF719
Your code initially works but R,5 does not get updated if R,3 is erased
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

The debugging module of the VB 6 IDE can be accessed by way of the Debug menu item. That menu item can normally be found in the IDE's main menu line as shown in this picture.   There is also a companion Debug Toolbar that looks like the followin…
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…
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
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…

763 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

Need Help in Real-Time?

Connect with top rated Experts

12 Experts available now in Live!

Get 1:1 Help Now