|
-
Apr 2nd, 2004, 05:54 PM
#1
Thread Starter
New Member
VBA for Excel -- Divide by Zero Error in Calculation
I have a form that calls a routine of calculations to be executed whenever the text in any input textbox is changed.
One of the calculations involves dividing, say, txtBox1 by txtBox2, with the answer going into a third box.
I rigged by using an IF statement to bypass the computation until there is something in txtBox2, but I know that there is some sort of IFERROR statement that would be better. Any ideas?
Thanks,
Jimbo.
-
Apr 2nd, 2004, 10:16 PM
#2
Can you post your code so we can help better.
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
-
Apr 3rd, 2004, 01:32 PM
#3
Lively Member
Are you looking for something like this:
IF ISERROR(txt1.value/txt2.value) then
msgbox ("AHHHHHH"
Else
txt3.value txt1.value/txt2.value
End if
Or do you want something that will post a zero (or even a null value to txt3 when you get a division by zero.
IF ISERROR(txt1.value/txt2.value) then
txt.value = null
Else
txt3.value txt1.value/txt2.value
End if
I would usually still code this using if statements. Anyone have any other suggestions? You can use the IIF statement for a one liner I guess.
-----
#VBA, VB 6 Professional Edition, Office XP Developper. Excel 97, Excel 2000, Excel XP
I miss my VIC 20.
Never should have upgraded to my commodore 64. ...
-
Apr 3rd, 2004, 02:59 PM
#4
VB Code:
If IsError(Text1.Text / Text2.Text) = True Then
MsgBox "Error"
End If
Is not the correct way of using the function. it
will generate a Runtime error 424. The function is suppossed to be used like...
If IsError(expression) Then...
The required expression argument must be a Variant of VarType vbError.
Why not just check for the text2.text being one character and it
being a zero. if it is a negative number that would be ok.
VB Code:
Private Sub Command1_Click()
If len(text2.text)=1 and instr(1,text2.text,"0")=1 Then
MsgBox "Divide by zero error"
text2.text = ""
Else
text3.text = Text1.Text / Text2.Text
End If
End Sub
VB/Office Guru™ (AKA: Gangsta Yoda™ ®)
I dont answer coding questions via PM. Please post a thread in the appropriate forum. 
Microsoft MVP 2006-2011
Office Development FAQ (C#, VB.NET, VB 6, VBA)
Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
If a post has helped you then Please Rate it! 
• Reps & Rating Posts • VS.NET on Vista • Multiple .NET Framework Versions • Office Primary Interop Assemblies • VB/Office Guru™ Word SpellChecker™.NET • VB/Office Guru™ Word SpellChecker™ VB6 • VB.NET Attributes Ex. • Outlook Global Address List • API Viewer utility • .NET API Viewer Utility •
System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6 
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|