Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using an IF statement without repeating a long formula

Can IF check and return the result from a formula without using it a second time inside the cell?

I am talking about using something like this:

IF( LARGE(A2:A21) > 0, LARGE(A2:A21), "No Data Entered")

This is an example/placeholder. My actual formula is a bit longer. I do not want to rewrite the formula (i.e. LARGE() ) within the IF function a second time.

Does anyone know if there is a way to do this?

like image 465
Sumit Mishra Avatar asked Nov 15 '25 18:11

Sumit Mishra


2 Answers

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)

Your Example

Original formula:

=IF( LARGE(A2:A7,2) > 0, LARGE(A2:A7,2), "No Data Entered")

Here's how it would work using LET so that you don't have to repeat the formula: Screenshot of LET formula

New formula:

=LET(runnerup,LARGE(A2:A7,2),IF(runnerup>0,runnerup,"No Data Entered"))

Explanation

We create a variable and use the LARGE 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: runnerup

Variable value: LARGE(A2:A7,2)

Calculation: IF(runnerup>0,runnerup,"No Data Entered")

Put together it looks like this:

=LET(runnerup,LARGE(A2:A7,2),IF(runnerup>0,runnerup,"No Data Entered"))

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)
like image 144
Josh Moore Avatar answered Nov 17 '25 09:11

Josh Moore


You can use an array form of LARGE() that will return an error if no value is greater than 0:

=IFERROR(LARGE(IF(A2:A21>0,A2:A21),1),"No Data Entered")

This would need to be confirmed with Ctrl-Shift-Enter instead of Enter when exiting edit mode.

like image 23
Scott Craner Avatar answered Nov 17 '25 10:11

Scott Craner



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!