# Looking for solution to shorten an IF formula

I have a long formula with the following structure:

``````=IF(!X!<>0,!X!+A1,"")
``````

where !X! is a very long formula.

Is there any solution to avoid repeating the !X! twice? I need it for two reasons:

1. To make the formula readable for user of the sheet
2. To reduce the process time

I appreciate any help.

Thanks, Dio

• if you want `To reduce the process time` then obviously you have to store the result somewhere, otherwise Excel has to recalculate the value again when the condition matches. Division is also a very costly operation Nov 24, 2017 at 13:35
• Just curious too, have you "golfed" the long formula down as much as you can? To make it shorter maybe add named ranges? Nov 25, 2017 at 0:52
• To make a long formula more readable, separate the arguments onto different lines using alt+return. One downside is unless the user expands the height of the formula bar, the formula looks incomplete. Nov 27, 2017 at 17:47
• Loosely related: Display Blank when Referencing Blank Cell in Excel Dec 8, 2017 at 4:19

Another approach is to use double inversion:

``````=IFERROR(1/(1/really_long_formula)+A1,"")
``````

If the really_long_formula evaluates to 0, you will get a divide by zero and the `IFERROR()` will catch it!

Please note that the usual way (and best way) to handle this requirement is with a helper cell.

• this may suffer from precision lost due to floating-point division Nov 24, 2017 at 13:31
• @LưuVĩnhPhúc You are completely correct! the AFH approach is the best approach! Nov 24, 2017 at 13:33
• This is a nice, dirty trick. I would refrain heavily from using it, but, as a last resort, who knows? Nov 25, 2017 at 5:30
• NOTE: it's true that there may be some precision loss, but it'd rarely be a relevant issue. I tested `1/1/x` with 100,000 random numbers several times in Excel, and only 1.4% of the time there was a rounding error. The maximum relative error I ever found was 1.6e-16, which is, well, effectively zero. Also, interestingly, integers never displayed rounding errors (tested several ranges from 0 to 1e14). So, this answer may be frowned upon from a purity/cleanness standpoint, but not from a practical calculation standpoint. Nov 25, 2017 at 19:22
• I find the Gary's solution as the best. First because it is clean (as mentioned also by André) and readable by other users. Second, because it satisfies my need. I do not have complicated or long integers. I hope the users, who choose this approach, read the comments to be awared about the pros and cons of this solution. Nov 27, 2017 at 11:46

The obvious answer is to put the formula in a work cell, away from the main sheet. For example, if you use `H1`, set it to:

``````=!X!
``````

``````=IF(H1<>0,H1+A1,"")
``````

This is typical of what one would do in any other programming language.

• I do not have possibility to add a help column. Thanks anyway. Nov 24, 2017 at 13:20
• @Dio if it's not supposed to be seen then simply hide the column. You can even name the column/range to make it easier to read Nov 24, 2017 at 13:32
• @Dio - OK. Your accepted answer is rather clever, but be aware that, because of rounding, you may lose accuracy (`1/(1/x)` is not always exactly the same as `x`), especially if you're dealing with integer data. It's also not applicable more generally (eg checking for less than zero). (Comment crossed with @LưuVĩnhPhúc's under accepted answer.)
– AFH
Nov 24, 2017 at 13:36
• Thanks for your advice and comments. I will leave the question still open to see if there is still new suggestions. Nov 24, 2017 at 13:44
• It would be helpful to see the long formula. Besides a work cell, it is possible to define a name in a worksheet that contains a formula Nov 24, 2017 at 18:21

Do you really need the result to be `""` in the false case? If you only need the cell to look blank (e.g. you won't use something like `=ISNUMBER()` on it later), you can use conditional formatting to hide the contents in the false case.

The conditional format you'll apply to the cell so that it doesn't display anything is the custom format `""`, like this (it's in Portuguese but you get the idea):

The formula in the cell will be, as expected, simply `=!X!+A1`.

The conditional formatting formula could be `=!X!=0`, but that would force recalculation of `!X!`, which you don't want (your "Point 2"). It's better to harness the cell itself by using `=B1=A1` (supposing our cell is B1) -- that would imply `!X! = 0`.

Even if you need the cell content to actually be `""`, usually minor alterations can be made in the worksheet so that this approach can be used. If that's the case, leave a comment describing the situation.

I do not have possibility to add a help column. Thanks anyway.

If you can't add a helper column, why not add a whole worksheet? There are several advantages to this:

• Your helper cell doesn't take up space that might otherwise be used, because it's in a separate worksheet.
• You can name the cell then address it by name, e.g. `=IF(X<>0,X+A1,"")`
• If you need to do this in more than one cell, you can:
1. Rename the helper worksheet "helper"
2. Put the helper equation in the same cell as the main equation (let's call it `D5`.
3. Address the cell as `helper!D5` in the main sheet.
• You can hide the helper sheet if necessary.
• It is faster than evaluating twice.
• It doesn't lose precision.

The disadvantages I can see are:

• You have to reference unnamed cells in the main sheet as `sheetname!D5` instead of just as `D5`.
• Now the formula is in two parts instead of one.
• Worksheet proliferation.

Weighing up the advantages and disadvantages, I think that for many use cases this is a good solution. There exist situations where it is not optimal, though I can't think of any at the moment.

• Great answer. The disadvantages I see are: 1. It doesn't help a lot with making the calculation simpler to the user, since now the big formula is hidden away. 2. Many workbooks are already crowded with sheets. Even if you hide it, you may have to go through lots of hidden sheets to unhide it. Other than that, it's a good solution. Nov 25, 2017 at 16:54
• @AndréNeves Thanks for that! That first one occurred to me, but I forgot it by the time I was writing the disadvantages section. Nov 25, 2017 at 18:41

an option not yet suggested is to create a user-defined function. You would need to turn on the Developer tab in the menu bar (google it) and create a module.

`````` public function udf_myCalc(ValueToAdd as double)
dim myvar as double
dim udf_myCalc as double
myvar = .. put the logic of !X! in here

if myvar<>0 then
else
``````=udf_myCalc(A1)