vba div 0 help

starl

Administrator
Joined
Aug 16, 2002
Messages
5,984
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
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

How to calculate loan payments in Excel?
Use the PMT function: =PMT(5%/12,60,-25000) is for a $25,000 loan, 5% annual interest, 60 month loan.

Damon Ostrander

MrExcel MVP
Joined
Feb 17, 2002
Messages
4,239
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.
 

starl

Administrator
Joined
Aug 16, 2002
Messages
5,984
Office Version
  1. 365
  2. 2019
Platform
  1. Windows
  2. MacOS
thanks, damon. your comments gave me some ideas!
 

Forum statistics

Threads
1,144,329
Messages
5,723,737
Members
422,512
Latest member
MHau5

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
Top