Results 1 to 4 of 4

Thread: VBA for Excel -- Divide by Zero Error in Calculation

  1. #1

    Thread Starter
    New Member PineyWoodsJimbo's Avatar
    Join Date
    Feb 2004
    Location
    Where the buffalo roam, USA
    Posts
    3

    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.

  2. #2
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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

  3. #3
    Lively Member TheFIDDLER's Avatar
    Join Date
    May 2002
    Location
    here and there and far away
    Posts
    126
    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. ...

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709
    VB Code:
    1. If IsError(Text1.Text / Text2.Text) = True Then
    2.         MsgBox "Error"
    3.     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:
    1. Private Sub Command1_Click()
    2.     If len(text2.text)=1 and instr(1,text2.text,"0")=1 Then
    3.         MsgBox "Divide by zero error"
    4.         text2.text = ""
    5.     Else
    6.         text3.text = Text1.Text / Text2.Text
    7.     End If
    8. 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 PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI 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
  •  



Click Here to Expand Forum to Full Width