Reputation: 1745
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.]
Upvotes: 57
Views: 195451
Reputation: 1134
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.
Upvotes: 44
Reputation: 54734
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)
Upvotes: 44
Reputation: 11
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.
Upvotes: 1
Reputation: 1
I know it's a little off-topic, but following up with the solution presented by Jonas Bøhmer, actually I think that MOD is the best solution to your example.
If your intention was to limit the result to one digit, MOD is the best approach to achieve it.
ie. Let's suppose that VLOOKUP(A1, B:B, 1, 0) returns 23. Your IF formula would simply make this calculation: 23 - 10 and return 13 as the result.
On the other hand, MOD(VLOOKUP(A1, B:B, 1, 0), 10) would divide 23 by 10 and show the remainder: 3.
Back to the main topic, when I need to use a formula that repeats some part, I usually put it on another cell and then hide it as some people already suggested.
Upvotes: 0
Reputation: 35
Not related to variables, your example will also be solved by MOD
:
=Mod(VLOOKUP(A1, B:B, 1, 0);10)
Upvotes: 1
Reputation: 100786
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
Upvotes: 2
Reputation: 87095
Yes. But not directly.
Simpler way
OR
Upvotes: 1
Reputation: 6670
There isn't a way to define a variable in the formula bar of Excel. As a workaround you could place the function in another cell (optionally, hiding the contents or placing it in a separate sheet). Otherwise you could create a VBA function.
Upvotes: 0
Reputation: 65421
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)
Upvotes: 7