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.
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.
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
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.![]()
Please Login or Register to view this content.
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.
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 theicon 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!)
It is happening on this line
![]()
Please Login or Register to view this content.
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?
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
How do I call this function from the main code?
![]()
Please Login or Register to view this content.
Thanks for your help. It is working now.
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
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.
There are currently 1 users browsing this thread. (0 members and 1 guests)
Bookmarks