+ Reply to Thread
Results 1 to 13 of 13

Run Time Error 11 (Division by Zero)

  1. #1
    Forum Contributor
    Join Date
    12-09-2006
    Posts
    229

    Run Time Error 11 (Division by Zero)

    I have this script that runs ok as lat long are not the same. When they are the same I get run time error 11 can't divide by zero. Is there a way to trap this error. Any help is appreciated.

    Please Login or Register  to view this content.

  2. #2
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,971

    Re: Run Time Error 11 (Division by Zero)

    What do you want to happen when this condition occurs?

    Edit: I couldn't figure out what line this was happening on. Where are you getting it?

    My first suggestion would be to detect it computationally before it happens. That is, something like

    Please Login or Register  to view this content.
    Another choice would be to detect it after it happens.
    Please Login or Register  to view this content.
    Last option is
    Please Login or Register  to view this content.
    which effectively ignores the divide by zero error, but that is probably not what you want, because the computation will leave the wrong result.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Forum Contributor
    Join Date
    12-09-2006
    Posts
    229

    Re: Run Time Error 11 (Division by Zero)

    Basically if this condition happens, the out put should be equal to zero. This means the distance between lat1, long1 and lat2, long2 are the same.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS 365 Family 64-bit 2502
    Posts
    26,971

    Re: Run Time Error 11 (Division by Zero)

    Quote Originally Posted by walid66 View Post
    Basically if this condition happens, the out put should be equal to zero. This means the distance between lat1, long1 and lat2, long2 are the same.
    What output? To give more specific advice I would need to know what line this is happening on. I only see two divisions. One is by 180. The other is in the Acos function

    -X / Sqr(-X * X + 1)

    The denominator can be zero only if X is 1 or -1. I haven't worked backwards far enough to determine when that can happen.

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Run Time Error 11 (Division by Zero)

    Shot in the dark...how about:
    Please Login or Register  to view this content.

    ...replaced with:
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  6. #6
    Forum Contributor
    Join Date
    12-09-2006
    Posts
    229

    Re: Run Time Error 11 (Division by Zero)

    It is happening on this line
    Please Login or Register  to view this content.

  7. #7
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Run Time Error 11 (Division by Zero)

    I figured that was where it was falling down normally.

    I was trying to get that function to be skipped altogether when the lat1/lat2 and long1/long2 values were the same. So you tried that and it still went into the function and failed?

  8. #8
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Run Time Error 11 (Division by Zero)

    Here's the function I use for central angle. It's not unhappy calculating distance between coincident points.
    Please Login or Register  to view this content.
    Entia non sunt multiplicanda sine necessitate

  9. #9
    Forum Contributor
    Join Date
    12-09-2006
    Posts
    229

    Re: Run Time Error 11 (Division by Zero)

    How do I call this function from the main code?

  10. #10
    Forum Expert shg's Avatar
    Join Date
    06-20-2007
    Location
    The Great State of Texas
    MS-Off Ver
    2010, 2019
    Posts
    40,689

    Re: Run Time Error 11 (Division by Zero)

    Please Login or Register  to view this content.

  11. #11
    Forum Contributor
    Join Date
    12-09-2006
    Posts
    229

    Re: Run Time Error 11 (Division by Zero)

    Thanks for your help. It is working now.

  12. #12
    Registered User
    Join Date
    01-09-2018
    Location
    Seoul, South Korea
    MS-Off Ver
    2013
    Posts
    1

    Re: Run Time Error 11 (Division by Zero)

    Please can someone help to locate the error. I'm running a VBA model and that pops up. The line in yellow is also being highlighted when i debug the error. Thanks in advance


    'gp 03-Apr-2012
    If optionHydraulGiven = "Volume" Then
    If i > 0 Then q(i) = (alp1(i) * Vol(i) / xl(i)) ^ (1 / (1 - bet1(i)))
    End If

    If geoMethod = "Depth" Then
    'use alp2/bet2 on Headwater and Reach sheets for depth:
    U(i) = alp1(i) * q(i) ^ bet1(i)
    depth(i) = alp2(i) * q(i) ^ bet2(i)
    Ac(i) = q(i) / U(i)
    b(i) = Ac(i) / depth(i)
    Btop(i) = b(i)
    Pwet = b(i) + 2# * depth(i)
    ElseIf geoMethod = "Width" Then
    'use alp2/bet2 on Headwater and Reach sheets for width:
    U(i) = alp1(i) * q(i) ^ bet1(i)
    b(i) = alp2(i) * q(i) ^ bet2(i)
    Ac(i) = q(i) / U(i)
    depth(i) = Ac(i) / b(i)
    Btop(i) = b(i)
    Pwet = b(i) + 2# * depth(i)
    Else
    MsgBox "Invalid entry in cell T8 of the Reach sheet. You must select either Width or Depth from the pull-down list."
    Sheets("Reach").Select
    Range("t8").Select
    End
    End If

  13. #13
    Forum Guru xlnitwit's Avatar
    Join Date
    06-27-2016
    Location
    London
    MS-Off Ver
    Windows: 2010; Mac: 16.13 (O365)
    Posts
    7,085

    Re: Run Time Error 11 (Division by Zero)

    Unfortunately your post does not comply with Rule 2 of our Forum RULES. Do not post a question in the thread of another member -- start your own thread.

    If you feel an existing thread is particularly relevant to your need, provide a link to the other thread in your new thread.

    Old threads are often only monitored by the original participants. New threads not only open you up to all possible participants again, they typically get faster response, too.
    Don
    Please remember to mark your thread 'Solved' when appropriate.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1