Still celebrating National IT Professionals Day with 3 months of free Premium Membership. Use Code ITDAY17

x
?
Solved

Divide by zero

Posted on 2006-10-25
3
Medium Priority
?
339 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
[X]
Welcome to Experts Exchange

Add your voice to the tech community where 5M+ people just like you are talking about what matters.

  • Help others & share knowledge
  • Earn cash & points
  • Learn & ask questions
3 Comments
 
LVL 76

Accepted Solution

by:
GrahamSkan earned 260 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 240 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

What does it mean to be "Always On"?

Is your cloud always on? With an Always On cloud you won't have to worry about downtime for maintenance or software application code updates, ensuring that your bottom line isn't affected.

Question has a verified solution.

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

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…
This article describes how to use a set of graphical playing cards to create a Draw Poker game in Excel or VB6.
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…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…
Suggested Courses

688 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