Solved

Divide by zero

Posted on 2006-10-25
3
333 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

NAS Cloud Backup Strategies

This article explains backup scenarios when using network storage. We review the so-called “3-2-1 strategy” and summarize the methods you can use to send NAS data to the cloud

Question has a verified solution.

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

Introduction I needed to skip over some file processing within a For...Next loop in some old production code and wished that VB (classic) had a statement that would drop down to the end of the current iteration, bypassing the statements that were c…
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
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…
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…

777 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