A SKU like TEE-OLIVE-M carries the colour and the size, but neither sits in a column you can filter or total. When every piece is a different length, cutting at the hyphens is the only rule that holds, and TEXTBEFORE and TEXTAFTER cut at hyphens directly.
You run stock for a small clothing shop, and Black Friday is in two days. The warehouse export in A2:B8 has only a SKU and the units on hand, and every SKU is built the same way: product, colour and size, joined by hyphens. The buyer wants to see the stock by colour and by size before deciding what to push in the sale. In C2, pull out the colour from the SKU in A2, and in D2, pull out the size. Copy both down to row 8. Then, in G3, total the units on hand for the colour named in G2, using the colour column you just built.
Solve it on your own to keep the bonus. Each hint gets one step closer to the formula.
This is the grid you start with. Cell references in the task — B6, C2 — point at the row numbers and column letters below.
| A | B | C | D | E | F | G | |
|---|---|---|---|---|---|---|---|
| 1 | SKU | On hand | Colour | Size | |||
| 2 | TEE-OLIVE-M | 14 | Colour | NAVY | |||
| 3 | HOOD-CHARCOAL-XL | 6 | Units on hand | ||||
| 4 | TEE-NAVY-S | 22 | |||||
| 5 | JKT-RUST-XXL | 3 | |||||
| 6 | BEANIE-NAVY-OS | 31 | |||||
| 7 | SWEAT-OATMEAL-L | 9 | |||||
| 8 | CREWSOCK-OLIVE-XS | 18 |
The pieces of a SKU have no fixed width, so any formula that counts characters is only right for the rows it was written against: RIGHT(A2,2) gives "-M" for a medium and "XL" for an extra large, and "XL" again for XXL. The hyphens are the only thing every row agrees on, and TEXTBEFORE and TEXTAFTER cut at a delimiter rather than at a position. For the size, TEXTAFTER(A2,"-",-1) uses instance -1, which means the last hyphen counting from the right, so it returns M, XL, XXL or OS however long the product and colour are. The colour needs two cuts, so the formula is built inside out: TEXTAFTER(A2,"-") drops the product and leaves OLIVE-M, and TEXTBEFORE of that at "-" drops the size and leaves OLIVE. Doing it in that order means each function only ever looks at the first hyphen of the text it is given, which is the simplest case. Once colour is a column of its own, it behaves like any other data: SUMIF(C2:C8,G2,B2:B8) matches NAVY against the T-shirt's 22 and the beanie's 31 and returns 53. That is the real payoff of splitting a code: a value buried inside a text string cannot be a SUMIF criterion, but a value in its own column can.