How to categorize text based on keywords in Excel
Excel formula
=XLOOKUP(TRUE,ISNUMBER(SEARCH($D$2:$D$6,A2)),$E$2:$E$6,"Other")Return the category of the first keyword found: =XLOOKUP(TRUE,ISNUMBER(SEARCH($D$2:$D$6,A2)),$E$2:$E$6,"Other").
- Result on the example
- one result per row in
B2, filled down toB7 - Works in
- Excel 2021, 2024 and Microsoft 365
- Google Sheets
- Different formula, see below
See it work
The formula is already in B2, filled down to B7. Change any input and the result updates, or edit the formula to experiment.
How it works
ISNUMBER(SEARCH($D$2:$D$6,A2)) builds a list of TRUE and FALSE, one per keyword: TRUE where the keyword appears in the transaction. XLOOKUP finds the first TRUE and returns the category on the same row, or "Other" when nothing matches.
=XLOOKUP(TRUE,ISNUMBER(SEARCH($D$2:$D$6,A2)),$E$2:$E$6,"Other")Result"Travel"
$D$2:$D$65 rows × 1 col: "uber", "starbucks", "amazon", "delta", …A range: 5 cells in one column.
A2"UBER *TRIP 4471"The value in A2.
SEARCH($D$2:$D$6,A2){5 rows × 1 col} 1, #VALUE!, #VALUE!, #VALUE!, …SEARCH: Position of text, not case-sensitive, allows wildcards.- find_text
$D$2:$D$65 rows × 1 col: "uber", "starbucks", "amazon", "delta", … - within_text
A2"UBER *TRIP 4471"
- find_text
ISNUMBER(SEARCH($D$2:$D$6,A2)){5 rows × 1 col} TRUE, FALSE, FALSE, FALSE, …ISNUMBER: TRUE if the value is a number.- value
SEARCH($D$2:$D$6,A2){5 rows × 1 col} 1, #VALUE!, #VALUE!, #VALUE!, …
- value
$E$2:$E$65 rows × 1 col: "Travel", "Meals", "Supplies", "Travel", …A range: 5 cells in one column.
XLOOKUP(TRUE,ISNUMBER(SEARCH($D$2:$D$6,A2)),$E$2:$E$6,"Other")"Travel"XLOOKUP: Searches one range and returns the matching item from another.- lookup_value
TRUE - lookup_array
ISNUMBER(SEARCH($D$2:$D$6,A2)){5 rows × 1 col} TRUE, FALSE, FALSE, FALSE, … - return_array
$E$2:$E$65 rows × 1 col: "Travel", "Meals", "Supplies", "Travel", … - if_not_found
"Other"
- lookup_value
In Google Sheets
Google Sheets needs a different formula: =ARRAYFORMULA(IFERROR(INDEX($E$2:$E$6,MATCH(TRUE,ISNUMBER(SEARCH($D$2:$D$6,A2)),0)),"Other")). On the same data it gives the same result. Here it is running in Google Sheets mode.
Other ways to do it
Each one was run on the same example sheet.
| Formula | Result | When to use it |
|---|---|---|
| =IFERROR(INDEX($E$2:$E$6,MATCH(TRUE,ISNUMBER(SEARCH($D$2:$D$6,A2)),0)),"Other") | Travel, Meals, Supplies… | Without XLOOKUP. In Excel 2019 and earlier, confirm it with Ctrl+Shift+Enter. |
Common mistakes
- The first keyword in the table wins. Put specific keywords above general ones ("uber eats" above "uber") so they are found first.
- Short keywords match inside other words: "delta" also matches "Deltaville Hardware". Use longer keywords or add spaces around them.
- An empty row in the keyword table matches every transaction. Keep the range exact, or use a Table so it grows with the list.
Try it yourself
Same data, empty answer cell. Your formula is graded instantly, then re-checked on changed data, so a typed-in number won't pass.
Categorize text based on keywords
B2, return the category of the first keyword in D2:D6 that appears in the transaction, or "Other" if none does. Fill down to B7.Questions
How do I list every matching category?
Filter instead of looking up: =TEXTJOIN(", ",TRUE,UNIQUE(FILTER($E$2:$E$6,ISNUMBER(SEARCH($D$2:$D$6,A2)),""))).
How do I make the keyword match case-sensitive?
Use FIND instead of SEARCH.
Checked by running on 2026-10-30: every formula and result on this page was calculated by the Spreadsheet Practice formula engine, in both Excel and Google Sheets modes.