40

Can't believe I don't know this, but is there a way to avoid repeating a formula in an if statement if the logical test is dependent on it?

i.e.

=IF((SUMIFS formula)=0,"",SUMIFs formula)

I want to replace that SUMIFS function in the false scenario with something short that will tell it to just programmatically repeat the formula it originally tested for. Repeating the formula twice has to have detrimental effects on processing speed. Negligible, maybe, but want to go for best-practices here. Thanks.

3
  • Better have the formula in one (hidden?) cell, and reference that cell in an equation like IF(cell=0,"",cell). It's a really good question but there isn't (as far as I know) a really good answer.
    – Floris
    Mar 12, 2014 at 17:38
  • Depends on your needs, but one more option is to apply custom format displaying empty string "" instead 0 Mar 12, 2014 at 17:39
  • 1
    I think we can all agree that the sensible choice is clearly =SUBSTITUTE(SUBSTITUTE("Z" & SUMIFS(...) & "Z", "Z0Z", ""), "Z", "")
    – Alex K.
    Mar 12, 2014 at 18:16

8 Answers 8

48

You can force an error like #DIV/0! and then use IFERROR, e.g.

=IFERROR(1/(1/SUMIFS_formula),"")

5
  • 4
    Wow, that is a really interesting solution for this scenario. Thanks. It is the most succinct out of all the answers here for the specific formula presented, but I only voted it up instead of choosing it as an answer because I actually would like some sort of variable. Repeating formulas comes up at times in Excel in various formula scenarios and I need a way to avoid doing that to pare down my formulas. Thanks though! I used this for my specific scenario.
    – Lorenzo
    Mar 12, 2014 at 21:00
  • This is an excellent trick. There are two clever things here: first, the use of ISERROR; second, rewriting your formula so it throws an error when the specific condition is true, and the "correct" value otherwise. Hat off to you!
    – Floris
    Mar 24, 2014 at 0:18
  • Nice solution for numeric data, but obvs won't work for text strings
    – geotheory
    Dec 12, 2017 at 14:19
  • This is evil and awesomely clever. Mar 18, 2020 at 0:18
  • 1
    As @GSerg answered for me, in 365 you can use the LET formula. =LET( x, SUMIFS(formula), IF(x=0,"",x))
    – Greg_D
    Apr 28, 2022 at 18:00
13

You can assign a Name to a formula and use the Name..............See:

Assigning a name to a formula

Relevant excerpt -

For example, let's suppose we frequently use a formula like: =SUM(A1:A100)-SUM(B1:B100) and this resides in A101 and is copied across many columns on row 101. It would be better in this case to create a custom formula that does this in each cell on row 101. Here is how;

1) Select cell A101 (this is vital).

2) Go to Insert>Name>Define and in the "Names in workbook" box type: SalesLessCosts

3) Now click in the "Refers to" box and type: =SUM(A1:A100)-SUM(B1:B100) then click Add.

Now you can replace the formula in cell A101 with: =SalesLessCosts. You can also copy this across row 101 and it will change its relative references just as the formula =SUM(A1:A100)-SUM(B1:B100) would. The reason it does this is all down to the fact we selected A101 before going to Insert>Name>Define and used relative references in =SUM(A1:A100)-SUM(B1:B100) when we added it to the "Refers to" box.

4
  • This is interesting. I for some reason didn't know you could declare unique formulas outside VB editor. Still seems strange to me there is no way to do this inside the formula, sort of like a "this" keyword in Javascript . Maybe in the future. Thanks.
    – Lorenzo
    Mar 12, 2014 at 21:03
  • I don't think this gets around the problem of evaluating the function twice, even if you saved a bit of typing. I like barry houdini's solution much better.
    – Floris
    Mar 24, 2014 at 0:21
  • Frankly, this tip is amazing. It doesn't get around doing it twice, but it's an incredible help! Jun 4, 2015 at 14:34
  • @LazzMaTazz It can save a lot of typing and mis-typing. Jun 4, 2015 at 14:36
8

The LET formula can be used for this exact scenario. You can define the formula as a variable and then within that same cell you can reference the variable in your formula.

The LET formula format looks like this:

=LET(name,name_value,calculation)

SUMIFS Example

Here's how it would work with your SUMIF example so that you don't have to repeat the formula:

Screenshot of LET example

In this screenshot we have an array A1:B7. We want to sum the values (Col B) if the name in ColA is "apple".

For this we have a standard SUMIFS formula of

=SUMIFS(B1:B7,A1:A7,"apple")

The formula is showing in E2. The result is shown in E3.

To put this into the IF statement without having to repeat the formula we can use LET as shown in the screenshot.

We create a variable with the SUMIFS formula as the value of that variable. We then write our IF statement using the variable name instead of rewriting the formula multiple times.

=LET(name,name_value,calculation)

Variable name: sumapples

Variable value: SUMIFS(B1:B7,A1:A7,"apple")

Calculation: IF(sumapples=0,"",sumapples)

Put together in the LET function it looks like this:

=LET(sumapples,SUMIFS(B1:B7,A1:B7,"apple"),IF(sumapples=0,"",sumapples))

This LET function can be used in any Excel formula, and is very useful for shortening long formulas that have repetition.


Optional: Extra complexity

If you want to you can get extra complicated by naming multiple variables.

=LET(name,name_value,name2,name_value2,calculation)
6

If all you need to do is hide zeroes, there is an easy way:

  • Select all cells where you wish to hide zeroes
  • Go into Custom Number Formatting
  • Set format to "General;General;"

The custom formatting has a structure of [positive numbers];[negative numbers];[zeroes] By making the last part blank you are effectively hiding zeroes, but showing everything else.

The advantage over conditional formatting is that you can use this on any background.

A neat trick which I sometimes use is to hide the cell value completely by using a custom format of ";;;". This way you can put images inside the cells, like the conditional formatting ones, and not see the value at all.

3

Try using the SUBSTITUTE function like this :

=SUBSTITUTE( VLOOKUP( H4; $D$5:$E$8; 2; 0 ); $H$1; $I$1 )

Here is an example:

Example

Here the formula I don't want to repeat twice is the VLOOKUP function. The result of VLOOKUP is a string found in another table (ex : "Green").

I want to check if that string matches a specific string value in $H$1 (here, "Yellow").

  • If it does, SUBSTITUTE replaces it with$I$1 (the error string you want. Here, "FORBIDDEN").
  • If it doesn't, it displays the VLOOKUP result string (the normal authorized output, like "Green").

This is useful for me because my actual formula is quite long, so I don't want to write it twice. I also dont want to use two different cells, because I'm already applying this formula on 10 columns, meaning I should add an extra 10 columns to make it work.

2
  • This is a good solution to remove 0 or NA from VLOOKUP. substitute(iferror(vlookup(),""),0,"") This formula: will return blank if the vlookup has error + change 0 to blank May 1, 2020 at 4:37
  • after this I realised this is a bad solution for use when you have 0 or numbers in the text! I had numbers removed by mistake!!! :( Don't use this if your text has numbers!! Jul 8, 2020 at 10:27
2

In some scenarios, MAX() or MIN() can do a wonderful job.

E.g., something like this:

=IF(SUMIFSformula>0,SUMIFSformula, 0)

Can be shortened to this:

=MAX(0,SUMIFSformula)

1

Since Excel 2007, the IFERROR statement does what the OP asked. From the help file:

Description: Returns a value you specify if a formula evaluates to an error; otherwise, returns the result of the formula. [italics mine]

Syntax: IFERROR(value, value_if_error)

I've since realised that this was already answered by @barry houdini above.

1
  • (SUMIFS formula)=0 does not necessarily result in an error. In fact, it most often won't.
    – GSerg
    Apr 28, 2022 at 17:45
0

Here is a hack - depending on whether you are just interested in the displayed value, or whether you need to use the value in another formula:

Put your SUMIF formula in the cell (without the IF part)

Create a conditional formatting rule which sets the font color to the background color when the cell value is 0

And hey presto, you get the desired result.

As I said - it's a hack, but it does prevent the double evaluation.

There is no "clean" solution that I am aware of.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge that you have read and understand our privacy policy and code of conduct.

Not the answer you're looking for? Browse other questions tagged or ask your own question.