The need: matching two tables
In everyday work, information is rarely in a single table. Orders give a product reference and a quantity; the price is in the catalog. Rather than copying prices by hand, you ask Excel to look up in the catalog the row that carries the same reference. That is the job of lookup functions.
The column that links the two tables is called the key (here Ref). It must be unique in the reference table: if P003 appears twice in the catalog, Excel will return the first occurrence without warning you.
RECHERCHEX
RECHERCHEX (XLOOKUP) is available in Microsoft 365, Excel 2021 and later versions, as well as in Excel for the web. Its syntax:
=RECHERCHEX(valeur_cherchée;plage_de_recherche;plage_de_résultat;[si_non_trouvé];[mode_correspondance])
For the price of the order on row 2:
=RECHERCHEX(B2;Produits!$A$2:$A$9;Produits!$D$2:$D$9)
Excel looks for the value of B2 in the references column and returns the value located on the same row in the prices column. Both ranges must have the same height. They are locked as absolute so that the formula can be copied down without the catalog “sliding”.
The optional fourth argument says what to display if the key is missing (by default, the #N/A error). The fifth sets the match mode: 0 exact (the default), -1 exact or the next smaller value, 1 exact or the next larger value, 2 wildcard characters.
RECHERCHEV, the ancestor
You will often come across RECHERCHEV (VLOOKUP) in existing workbooks:
=RECHERCHEV(B2;Produits!$A$2:$E$9;4;FAUX)
It looks for B2 in the first column of the table and returns the 4th column. Two traps: forgetting FAUX (FALSE) as the last argument (the lookup becomes approximate and can return a wrong price with no visible error), and inserting a column in the table, which shifts the number 4. RECHERCHEX has neither of these flaws.
INDEX and EQUIV
The INDEX/EQUIV (INDEX/MATCH) pair works in all versions of Excel. EQUIV returns the position of a value in a range; INDEX returns the value located at a given position:
=EQUIV("P007";Produits!$A$2:$A$9;0) -> 7
=INDEX(Produits!$B$2:$B$9;7) -> Caisson 3 tiroirs
=INDEX(Produits!$B$2:$B$9;EQUIV("P007";Produits!$A$2:$A$9;0))
The 0 in EQUIV enforces an exact match: never forget it. Since the two ranges are independent, the result column can be to the left of the key, which RECHERCHEV cannot do. INDEX also accepts a row and a column: with two EQUIV, you get a two-way lookup (a reference and a column name).
Approximate lookup: threshold grids
Some tables do not contain every possible value but thresholds: a commission grid (0 €: 0 %, 2 000 €: 3 %, 4 000 €: 5 %, 6 000 €: 7 %). For revenue of 4 400 €, you want the rate of the 4 000 threshold, that is the equal or next smaller value:
=RECHERCHEX(B2;Grille!$A$2:$A$5;Grille!$B$2:$B$5;;-1)
The fourth argument is left empty (two consecutive semicolons) and mode -1 does the rest. A revenue exactly equal to a threshold (4 000) does get the rate of that threshold.
Exercises
Solutions and tests stay on the server. In the student space, your code is run and checked, and the tutor gives hints before the solution.
Value orders with RECHERCHEX
Create the file
C:\Formation\produits.csvwith Notepad, paste in the content below, save it, then open it in Excel by double-clicking it. The data occupies the rangeA1:E9. Rename the sheet toProduits. If everything lands in column A, use Data > Text to Columns with the semicolon as the delimiter.Ref;Designation;Famille;PrixHT;Stock P001;Ecran 24 pouces;Informatique;149;12 P002;Clavier sans fil;Informatique;39;40 P003;Fauteuil;Mobilier;229;5 P004;Bureau droit;Mobilier;310;3 P005;Lampe LED;Eclairage;45;18 P006;Casque audio;Informatique;79;0 P007;Caisson 3 tiroirs;Mobilier;139;7 P008;Multiprise;Electricite;19;55In the same workbook, add a sheet named
Commandesand enter (or paste then convert) the following data in the rangeA1:C5:NumCde;Ref;Quantite C101;P003;2 C102;P008;10 C103;P001;4 C104;P006;1In the
Commandessheet:- In D1, enter
PrixHT; in D2, write a RECHERCHEX that returns the price of the reference in B2 from theProduitssheet. Copy it down to D5. - In E1, enter
Montant; in E2, calculate quantity x price, then copy it down. - In E6, calculate the total of the amounts.
- The Produits sheet ranges are absolute references
- D2 displays 229 and D5 displays 79
- E4 displays 596
- E6 displays 1323
- In D1, enter
INDEX/EQUIV in both directions
On the previous workbook, in a new sheet
Recherches:- In A1, enter
P007. In B1, display the designation of this reference with INDEX and EQUIV. - In A2, enter
Lampe LED. In B2, display its reference (the column located to the left of the designation). - In A3, enter
P004and in A4Stock. In B3, write a two-way INDEX formula that returns the value of the column named in A4 for the reference in A3. - Replace A4 with
PrixHTand observe B3.
- Every formula uses EQUIV with 0 as the last argument
- B1 displays Caisson 3 tiroirs
- B2 displays P005
- B3 displays 3, then 310 when A4 is PrixHT
- In A1, enter
Commission grid
Create the file
C:\Formation\grille.csvwith Notepad, paste in the content below, save it, then open it in Excel by double-clicking it. The data occupies the rangeA1:B5. Rename the sheet toGrille. If everything lands in column A, use Data > Text to Columns with the semicolon as the delimiter.Seuil;Taux 0;0% 2000;3% 4000;5% 6000;7%In the same workbook, add a sheet named
Commissionsand enter (or paste then convert) the following data in the rangeA1:B4:Commercial;CA Martin;4400 Bernard;3700 Petit;4000In the
Commissionssheet:- In C1, enter
Taux; in C2, write a RECHERCHEX in mode-1that returns the grid rate matching the revenue (CA). Copy it down to C4 and format as a percentage. - In D1, enter
Commission; in D2, calculate CA x rate and copy it down. - In D5, total the commissions.
- C2 (Martin, 4400) displays 5 %, C3 (Bernard, 3700) displays 3 %
- C4 (Petit, 4000, exactly a threshold) displays 5 %
- D2 = 220, D3 = 111, D4 = 200
- D5 displays 531
- In C1, enter
RECHERCHEV without FAUX
What is the risk of writing
=RECHERCHEV(B2;Produits!$A$2:$E$9;4)without a fourth argument?- None, the lookup is exact by default
- An approximate lookup that can return a wrong price without displaying an error
- A #NOM? error
- The formula looks in the 4th row
End-of-lesson quiz
Which RECHERCHEX argument lets you display text when the key is missing?
- The third
- The fourth (si_non_trouvé)
- The sixth
What does EQUIV(“P003”;A2:A9;0) return if P003 is in A4?
- 4
- 3
- P003
For a threshold grid, which RECHERCHEX match mode should you use?
- 0
- -1
- 2
The rest of the course
- Nested SI (IF), SI.CONDITIONS (IFS), ET (AND) and OU (OR)20 min
- Conditional Sums and Counts: SOMME.SI.ENS (SUMIFS), NB.SI.ENS (COUNTIFS)20 min
- Text and Date Functions20 min
- Handling Errors: SIERREUR (IFERROR) and SI.NON.DISP (IFNA)15 min