vba div 0 help

starl

Administrator
Joined
Aug 16, 2002
Messages
6,081
Office Version
  1. 365
Platform
  1. Windows
Got vb code that eventual does some calculations. Some of these may results in 0/0.. which causes an error. is there another function I could use that would get around this?
other than error handling, I could use if statements - but do I have any other choice?

thanks
 

Excel Facts

Create a chart in one keystroke
Select the data and press Alt+F1 to insert a default chart. You can change the default chart to any chart type
Hi starl,

There are a number of ways of "handling" these types of errors, but there is no "best" method. The best method depends on what your function does, how you expect to use it, and what the cause of the 0/0 is. Here are some examples:

1. you are computing a value that has a "limit value" (a la L'Hospital's rule), such as sin(x)/x, which approaches 1 as x approaches 0. In this case since the 0/0 should in fact yield a 1, you should use an IF test to check for x = 0 and yield a 1.

2. your function will only be called as a UDF from a worksheet cell, and never from another VBA function or macro. In this case you might want to have the function yield a #DIV/0! error. You can do this by using an error handler to trap the error and then assigning CVErr(xlErrDiv0) to the function return value. The function must yield a Variant data type in order to do this.

3. Your function will only be called from another VBA function or macro. In this case you might want to consider assigning the function return value to Null. This will enable the calculations to proceed without a fatal error or any exception handling, and all calculations that use this value will also yield a Null result. As in (2) above this requires that the function yield a Variant result. This can be a good way to handle calculations that have a range of validity, but you don't want input values that are outside this range to cause a fatal error that stops other valid calculations from occurring.

If you want to post your code, or describe what it calculates, a response that is more tailored to your question might be possible.
 
Upvote 0
thanks, damon. your comments gave me some ideas!
 
Upvote 0

Forum statistics

Threads
1,214,584
Messages
6,120,387
Members
448,956
Latest member
JPav

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top