I hate repeating functions, particularly in Excel formulas. Is there any way that I can avoid something like:
=IF( VLOOKUP(A1, B:B, 1, 0) > 10, VLOOKUP(A1, B:B, 1, 0) - 10, VLOOKUP(A1, B:B, 1, 0) )
[The above is just a simple example of the problem, and not a particular formula that I'm working with.]
Variable cells (Changing cells or Adjustable cells in earlier versions) are cells that contain variable data that can be changed to achieve the objective. Excel Solver allows specifying up to 200 variable cells.
Go to the Formulas tab and select Name Manager. There you'll see all the cells that have been assigned a name. The current value is displayed under “Value.” You can find the reference cell in the “Refers To” column. The Scope column tells you the scope of that named variable.
You could define a name for the VLOOKUP part of the formula.
=VLOOKUP(A1,B:B, 1, 0)
=IF( Value > 10, Value - 10, Value )
Step (1) is important here: I guess on the second row, you want Excel to use VLOOKUP(A2,B:B, 1, 0)
, the third row VLOOKUP(A3,B:B, 1, 0)
, etc. Step (4) achieves this by using relative references (A1
and B:B
), not absolute references ($A$1
and $B:$B
).
Note:
For newer Excel versions with the ribbon, go to Formulas ribbon -> Define Name. It's the same after that. Also, to use your name, you can do "Use in Formula", right under Define Name, while editing the formula, or else start typing it, and Excel will suggest the name (credits: Michael Rusch)
Shortened steps: 1. Right click a cell and click Define name... 2. Enter a name and the formula which you want to associate with that name/local variable 3. Use variable (credits: Jens Bodal)
Now you can use the function LET to declare variables within Excel formulas. This function is available since Jun 2020 for Microsoft 365 users.
Given your example, the formula will be:
=LET(MyFunc,VLOOKUP(A1,B:B,1,0), IF(MyFunc > 10, MyFunc - 10, MyFunc ) )
The 1st argument is the variable name and the 2nd argument is the function or range. You can add more pairs of arguments variable, function/range.
After adding the variables, the last argument will be your formula of interest -- calling the variables you just created.
For more information, please access the Microsoft webpage here.
You could store intermediate values in a cell or column (which you could hide if you choose)
C1: = VLOOKUP(A1, B:B, 1, 0)
D1: = IF(C1 > 10, C1 - 10, C1)
Two options:
VLOOKUP
function in its own cell: =VLOOKUP(A1, B:B, 1, 0)
(in say, C1), then formula referencing C1: =IF( C1 > 10, C1 - 10, C1 )
Function MyFunc(a1, a2, a3, a4)
Dim v as Variant
v = Application.WorksheetFunction.VLookup(a1, a2, a3, a4)
If v > 10 Then
MyFunc = v - 10
Else
MyFunc = v
End If
End Function
Yes. But not directly.
Simpler way
OR
Defining a NAME containing the lookup is a neat solution, HOWEVER, it always seems to store the sheet name with the cell reference. However, I think if you delete the sheet name in the '' quotes but leave the "!", it may work.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With