How to categorize text strings based on keywords in Excel?
When working with large sets of data in Excel, you might often need to organize text entries based on specific keywords, helping to streamline reporting or analysis processes. For instance, assume you have a list of various text strings in Column A. You wish to categorize each entry depending on keywords listed in Column D — such as grouping any cell containing "Kutools" under "Extendoffice", or assigning any string mentioning "football" or "basketball" to the "Sport" category. The following screenshot illustrates such a scenario. Dealing with this task manually can be time-consuming and error-prone, especially as your dataset grows. In this article, we explore practical and efficient methods to automatically categorize these text strings, aiding quick data organization and improved workflow quality in Excel.

Categorize list of text strings based on keywords with array formula
Categorize text strings based on keywords with VBA code
Categorize list of text strings based on keywords with array formula
You can use an array formula to automatically assign categories to text strings based on matching keywords, which is especially practical in large data sets where manual sorting is unfeasible. Array formulas enable powerful pattern-matching capabilities in Excel, letting you create a dynamic link between data and keyword groups. This is helpful in scenarios such as automatically labeling incoming customer feedback, organizing inventory, or sorting emails based on content.
1. Lay out your data as needed, typically with text strings (to be categorized) in Column A, keywords in Column D, and corresponding categories in Column E. Refer to the example screenshot below for a sample dataset arrangement.

2. Access a blank cell adjacent to your original data (for example, begin in cell B2 if your strings are in A2:A10) and input the following array formula:
- Tips: In this formula:
- $E$2:$E$8: is the range containing the category names to be assigned to each text string;
- $D$2:$D$8: is the column containing keywords you want to match within your text strings;
- A2: is the cell with the text string to be analyzed and categorized.
For best results, ensure the keyword list and corresponding categories are aligned row-wise and there are no blank cells within your lookup ranges. Double-check for typos or extra spaces in keywords to prevent mismatches.
3. Press Ctrl + Shift + Enter (instead of just Enter) after inputting the formula. Excel will wrap the formula in curly brackets indicating an array formula. This will generate the appropriate category for the first string. Then, use the fill handle to drag the formula down along the column, so all entries in your list are auto-categorized. See screenshot:

When using array formulas, it's crucial to use the correct key combination, and when copying the formula, ensure that you're not breaking the array structure. If your data range expands, update the ranges in your formula for accurate categorization.

Unlock Excel Magic with Kutools AI
- Smart Execution: Perform cell operations, analyze data, and create charts—all driven by simple commands.
- Custom Formulas: Generate tailored formulas to streamline your workflows.
- VBA Coding: Write and implement VBA code effortlessly.
- Formula Interpretation: Understand complex formulas with ease.
- Text Translation: Break language barriers within your spreadsheets.
Categorize text strings based on keywords with VBA code
If you regularly need to categorize large volumes of text strings and want a flexible, automated solution, VBA offers a way to dynamically process and assign categories based on keyword matches. This approach is suited for advanced users who wish to tailor their categorization rules or integrate them with other automated workflows. A VBA macro can dramatically reduce manual overhead and introduce robust error handling for custom keyword-category pairings.
1. Open the VBA Editor in Excel by clicking Developer Tools > Visual Basic. In the popup window, go to Insert > Module to create a new module. Copy and paste the following VBA code into the module:
Sub CategorizeByKeywords()
Dim cell As Range
Dim keywordRng As Range
Dim categoryRng As Range
Dim i As Integer
Dim lastRowA As Long
Dim lastRowD As Long
Dim matchFound As Boolean
On Error Resume Next
xTitleId = "KutoolsforExcel"
lastRowA = Cells(Rows.Count, "A").End(xlUp).Row
lastRowD = Cells(Rows.Count, "D").End(xlUp).Row
Set keywordRng = Range("D2:D" & lastRowD)
Set categoryRng = Range("E2:E" & lastRowD)
For Each cell In Range("A2:A" & lastRowA)
matchFound = False
For i = 1 To keywordRng.Rows.Count
If InStr(1, cell.Value, keywordRng.Cells(i, 1).Value, vbTextCompare) > 0 Then
cell.Offset(0, 1).Value = categoryRng.Cells(i, 1).Value
matchFound = True
Exit For
End If
Next i
If Not matchFound Then
cell.Offset(0, 1).Value = "" ' Assign blank if no keyword matched
End If
Next cell
End Sub 2. To run the macro, click the
"Run" button, or press F5 in the VBA window. The macro will process all entries in column A starting from row 2, searching for keywords listed in column D, and write the corresponding category from column E into column B.
Remember to adjust keyword/category ranges if your list exceeds the shown example. If needed, customize the macro to handle overlapping or multiple keyword matches, or provide default categories for unmatched items. Always back up your data before running new code, and save your workbook as macro-enabled format (.xlsm) for future reuse.
Download sample file
Click to download the sample file!
More relative text category articles:
- Categorize Data Based On Values In Excel
- Supposing, you need to categorize a list of data based on values, such as, if data is greater than90, it will be categorized as High, if is greater than60 and less than90, it will be categorized as Medium, if is less than60, categorized as Low, how could you solve this task in Excel?
- Assign A Value Or Category Based On A Number Range
- This article is talking about assigning value or category related to a specified range in Excel. For example, if the given number is between0 and100, then assign value5, if between101 and500, assign10, and for range501 to1000, assign15. Method in this article can help you get through it.
- Calculate Or Assign Letter Grade In Excel
- To assign letter grade for each student based on their scores may be a common task for a teacher. For example, I have a grading scale defined where the score0-59 = F,60-69 = D,70-79 = C,80-89 = B, and90-100 = A as following screenshot shown. In Excel, how could you calculate letter grade based on the numeric score quickly and easily?
- Assign Serial Number To Duplicate Or Unique Values
- If you have a list of values which contains some duplicates, is it possible for us to assign sequential number to the duplicate or unique values? It means giving a sequential order for the duplicate values or unique values. This article, I will talk about some simple formulas to help you solving this task in Excel.
The Best Office Productivity Tools
Kutools for Excel Solves Most of Your Problems, and Increases Your Productivity by 80%
- Super Formula Bar (easily edit multiple lines of text and formula); Reading Layout (easily read and edit large numbers of cells); Paste to Filtered Range...
- Merge Cells/Rows/Columns and Keeping Data; Split Cells Content; Combine Duplicate Rows and Sum/Average... Prevent Duplicate Cells; Compare Ranges...
- Select Duplicate or Unique Rows; Select Blank Rows (all cells are empty); Super Find and Fuzzy Find in Many Workbooks; Random Select...
- Exact Copy Multiple Cells without changing formula reference; Auto Create References to Multiple Sheets; Insert Bullets, Check Boxes and more...
- Favorite and Quickly Insert Formulas, Ranges, Charts and Pictures; Encrypt Cells with password; Create Mailing List and send emails...
- Extract Text, Add Text, Remove by Position, Remove Space; Create and Print Paging Subtotals; Convert Between Cells Content and Comments...
- Super Filter (save and apply filter schemes to other sheets); Advanced Sort by month/week/day, frequency and more; Special Filter by bold, italic...
- Combine Workbooks and WorkSheets; Merge Tables based on key columns; Split Data into Multiple Sheets; Batch Convert xls, xlsx and PDF...
- Pivot Table Grouping by week number, day of week and more... Show Unlocked, Locked Cells by different colors; Highlight Cells That Have Formula/Name...

- Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
- Open and create multiple documents in new tabs of the same window, rather than in new windows.
- Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
