Hey guys,
I use a lot of VLOOKUPs for my job, when I paste in a CSV export from a central dashboard. This works fine, except when they change the dashboard's column order unexpectedly, which obviously scrambles the VLOOKUPs and forces me to modify them.
I realize the standard solution to this problem is to switch to INDEX and MATCH, but the formulas are always so bloated and it's a bit more mental effort to double check that the rows and columns are accurate.
So with the help of Copilot, I decided to make my own version of VLOOKUP that accepts a column name instead of a fixed number. I call it TLOOKUP.
Usage: =TLOOKUP(RowName, Table, ColumnName)
Example
| Item |
Type |
Price |
| Apple |
Fruit |
$1.00 |
| Orange |
Fruit |
$2.00 |
| Banana |
Fruit |
$3.00 |
| Carrot |
Vegetable |
$1.50 |
| Steak |
Meat |
$20.00 |
To get the price of an Apple, you'd just use =TLOOKUP("Apple",A1:C6,"Price")
To me this just seems like a more elegant solution than INDEX and MATCH, or nested XLOOKUPs.
INDEX and MATCH: =INDEX(A2:C6, MATCH("Apple", A2:A6, 0), MATCH("Price", A1:C1, 0))
Nested XLOOKUPs: =XLOOKUP("Apple", A2:A6, XLOOKUP("Price", A1:C1, A2:C6))
VLOOKUP and MATCH: =VLOOKUP("Apple", A1:C6, MATCH("Price", A1:C1, 0), FALSE)
The LAMBDA is here if you want to add TLOOKUP to your own spreadsheet:
=LAMBDA(rowName,tbl,colName,
INDEX(
tbl,
MATCH(rowName,INDEX(tbl,0,1),0),
MATCH(colName,INDEX(tbl,1,0),0)
)
)
I've started using this on my spreadsheets and work and it appears to be returning the same correct values as VLOOKUP. I chose not to include error handling because VLOOKUP returns an error if a lookup fails, so I wanted the same behavior from TLOOKUP. I don't see any obvious downsides but I could be missing something.
Any theories on why a formula like this wouldn't be implemented natively in Excel?
Edit: I know that this is effectively just INDEX and MATCH. But isn't =TLOOKUP("Apple",A1:C6,"Price") more simple and readable than =INDEX(A2:C6, MATCH("Apple", A2:A6, 0), MATCH("Price", A1:C1, 0))?
Edit 2: My main motivation for wanting a custom function is that my data source is a CSV export (that I don't have control over) with columns that often change order. If the columns were consistent this wouldn't be needed.