Excel VBA Course
Excel VBA Course - From Beginner to Expert

200+ Video Lessons
50+ Hours of Video
200+ Excel Guides

Become a master of VBA and Macros in Excel and learn how to automate all of your tasks in Excel with this online course. (No VBA experience required.)

View Course

#DIV/0! Problem

0

I have a spreadsheet where I need to tally a range of quantities together with values. 

For example

Product A - qty 0 - value $5.00 each 

Product B - qty 6 - value $10.00 each

Product C - qty 3 - value $6.50 each

Product D - qty 0 - value $ 25.00 each

I have a "Total" column across the sheet. The formula in that Total column looks like this (but much longer):

((H6/F6)-$H$2)*F6+((K6/I6)-$K$2)*I6+((N6/L6)-$N$2)*L6

Where the division is necessary, the are some "0" quantities, but within the string, there are also some products with quantities. 

My question is, how do I receive a value total in my total column? 

I hope and am sure,  that there is an answer to this. 

Answer
Discuss

Answers

0
Selected Answer

I want to suggest three ways of resolving the division error in ((H6/F6)-$H$2)*F6 if F6 evaluates to zero.

  1. =((H6/IF(F6,F6,1))-$H$2)*F6

    This is the most direct approach. It just avoids the use of zero. In your case I think it may not work because H6-H2 might produce an unacceptable result.
  2. =IFERROR(((H6/F6)-$H$2),0)*F6

    This formula allows the error to occur but substitutes 0 for the result if there is an error. The first part of the formula returns zero in case of error and zero multiplied by zero (in F6) will deliver a result of zero.
    Of course, this looks convoluted. Therefore the ISERROR() function might be placed differently. In this case the result =0 if any error occurs in the formula.
    =IFERROR(((H6/F6)-$H$2)*F6,0)
  3. The underlying logic, "return 0 if F6 = 0", gives raise to the third solution which is its direct interpretation in Excel syntax.
    =IF(F6,((H6/F6)-$H$2)*F6,0)

Note that the expression IF(F6, ... is paramount to IF(F6<>0, ... IF(F6 returns True if F6 holds any number other than zero.

Of course, you would need to replicate this formula for each row and just add up the results of each as you are now doing. However that doesn't seem like the best possible solution. Normally, one would have a column for a line total and a sum of the line totals at the end of that column. A running total on each row would be constructed the same way. But if you are trying to do without a column for line total (even a hidden one) one would use the SUMPRODUCT() function to make the total or an array formula. Perhaps, since it doesn't really relate to the subject here, you like to make that your question in another question.

Discuss

Discussion

Thank you. The first option presented worked !
I am most grateful.
Kevin (rep: 2) Mar 1, '20 at 9:07 pm
Add to Discussion


Answer the Question

You must create an account to use the forum. Create an Account or Login