Shorter IF formula when condition and Value-If-True are the same

mrblister

Board Regular
Joined
Nov 20, 2016
Messages
191
Office Version
  1. 2019
Platform
  1. Windows
This happens to me once in a while, and I'm wondering if there's a simple way around it.

The standard If-then function is: If(Condition, True, False).
But sometimes the Condition and the True-path are the same. For example, I want to test if cell B1 is 5. If so, that's the answer, otherwise so whatever else. ie IF(B1=5, B1, C5).

It becomes an issue if the condition is huge (with long, nested formula), you are then writing the same formula twice. E.g. IF([very_long_formula_A]<>0, [very_long_formula_A], 2).

Is there a way around this so you don't need to write the same formula twice?
 

Excel Facts

Why are there 1,048,576 rows in Excel?
The Excel team increased the size of the grid in 2007. There are 2^20 rows and 2^14 columns for a total of 17 billion cells.
One way is to use a "helper" column to do the calculation, then the IF can reference the "helper" column addess instead of having the whole long formula.

There may be other options too, depending on the relationship between the values in the cells.
Can you tell us a little bit about what is in the two cells?
Is there any pattern?
You might be able to use something like a MAX or MIN function, if there is a certain pattern or rule that will always be true.
 
Upvote 0
Is there a way around this so you don't need to write the same formula twice?
The only consistent way around it is to use the LET function which you will not have in excel 2019.
There may be possible workarounds depending on the specific long formula and whether or not there are known limits to the potential results, but there would be no generic fix.
 
Upvote 0
If you're checking to see if the result of [very long formula] is 0, you could try this ugly hack clever trick:

Excel Formula:
=IFERROR(1/(1/[very long formula]),2)

The reciprocal of 0 gives a #DIV/0! error, which triggers the IFERROR. If it's non-zero, the second reciprocal changes it back. You could also use EXP(LN( to check for negative numbers, but that's really out there.
 
Upvote 0
Consider
a) Let which is available in Excel 365 or
b) the UDF V(). I first saw this UDF on the forum in about 2002.
Let is more advanced than the UDF V.
If a value is used multiple times in a formula and it is long, using the available Let or V() will make the formula more efficient and/or easier to read. With this overly simple example, the first example calculates the sum 3 times versus E3 and E4 that calculate the sum once.
As mentioned above, alternatives including a helper cell can be considered.

T202108b.xlsm
ABCDE
1
2ProductAmount142.80
3A10With UDF V()142.80
4B12With LET Excel 365142.80
5C14
6D16
7E18
8F20
9G22
10H24
2bb
Cell Formulas
RangeFormula
E2E2=IF(SUM(C3:C10)>100,SUM(C3:C10)*1.05,SUM(C3:C10)*0.95)
E3E3=IF(V(SUM(C3:C10))>100,V()*1.05,@V()*0.95)
E4E4=LET(Amt, SUM(C3:C10), IF(Amt>100,Amt*1.05,Amt*0.95))


The UDF follows
VBA Code:
Public Function V(Optional vrnt As Variant) As Variant
' Stephen Dunn
'
Static vrntV As Variant
If Not IsMissing(vrnt) Then vrntV = vrnt
V = vrntV
End Function
 
Upvote 0
Is there any pattern?
[very_long_formula_A] appears twice; in the Condition and the If_True (or If_False), so trying to reduce that to one instance.

The IF Condition varies, but variants that seem to appear often are: [very_long_formula_A]=[some_value] (or >, <>, ect.), and [very_long_formula_A]=0.
 
Last edited:
Upvote 0
@Dave Patton thanks for the examples. Never seen "Let" in action before; hopefully it will come to desktop Excel in the future.
 
Upvote 0
hopefully it will come to desktop Excel in the future.
Not for existing versions but it will possibly be in the next version, based on the release cycle of previous versions that should be excel 2022 which should be due out in the next month or so. I haven't seen anything to confirm this but it may have been announced somewhere.
 
Upvote 0
If you're checking to see if the result of [very long formula] is 0, you could try this ugly hack clever trick:

Excel Formula:
=IFERROR(1/(1/[very long formula]),2)

The reciprocal of 0 gives a #DIV/0! error, which triggers the IFERROR. If it's non-zero, the second reciprocal changes it back. You could also use EXP(LN( to check for negative numbers, but that's really out there.
OOOHH. That IS a clever trick! Yeah If [very long formula] is 0 is a commonly recurring variant case for me.

Hmmm... Let's see if we can expand on this.

Check result IF [very_long_formula_A]=[some_value].
For example, if [some_value]=2.
Then this SHOULD work:
Excel Formula:
IFERROR(1/(1/([very long formula]-2)),2)

Check result IF [very_long_formula_A]>[some_value].
For example, if [some_value]>2.
Then this SHOULD work:
Excel Formula:
IFERROR(1/(1/(max(2-[very long formula],0))),2)

And so on. Thanks a bunch!
 
Upvote 0

Forum statistics

Threads
1,215,454
Messages
6,124,933
Members
449,195
Latest member
Stevenciu

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