Count Phrase Occurrences in Excel Cells with VBA
Question details
Count how many times a search phrase appears in each cell in H2:H1000 and write the counts to I2:I1000 using VBA.

- Product
- Microsoft Excel
- Device & OS
- not provided
- Scenario
- A worksheet contains text in column H, and the user wants a separate occurrence count for each row.
- Observed behavior
- The desired result is a count in each corresponding cell in column I, based on the phrase entered in I1.
Open the workbook in desktop Excel and confirm that H2:H1000 contains the text to search. Make sure I2:I1000 is available for the results, because the macro will overwrite those cells.
Run a VBA macro to count the phrase in each cell
Use this macro to search every cell in H2:H1000 and write its count to the corresponding row in column I.
The macro compares the length of each cell's text before and after removing the search phrase. It counts non-overlapping matches and is case-sensitive.
On the worksheet you want to search, enter the phrase in cell I1. Leave the phrase exactly as you want it matched, including any spaces.
In desktop Excel, choose Developer > Visual Basic. On Windows, you can also press Alt+F11.
In the VBA editor, select Insert > Module and paste this code: Sub CountPhraseOccurrences() Dim ws As Worksheet Dim searchWord As String Dim cellText As String Dim i As Long Set ws = ActiveSheet If IsError(ws.Range("I1").Value2) Then MsgBox "Enter a valid search phrase in I1." Exit Sub End If searchWord = CStr(ws.Range("I1").Value2) If Len(searchWord) = 0 Then MsgBox "Enter a search phrase in I1." Exit Sub End If For i = 2 To 1000 If IsError(ws.Cells(i, 8).Value2) Then ws.Cells(i, 9).Value = CVErr(xlErrValue) Else cellText = CStr(ws.Cells(i, 8).Value2) ws.Cells(i, 9).Value = (Len(cellText) - Len(Replace(cellText, searchWord, "", 1, -1, vbBinaryCompare))) / Len(searchWord) End If Next i End Sub
Return to the worksheet you want to search, then choose Developer > Macros, select CountPhraseOccurrences, and click Run. Check the counts in I2:I1000.
To keep the VBA code in the workbook, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm).

Count phrases with a formula in WPS Spreadsheet
WPS Office is a free, lightweight, easy-to-use alternative with a familiar interface for working with spreadsheets. It can open Excel files for a smooth transition; use a LEN and SUBSTITUTE formula to count phrases without relying on the Excel VBA macro.

Frequently Asked Questions
What happens if I1 is blank?
The macro displays a message and stops before calculating, preventing division by zero. Enter a phrase in I1, then run CountPhraseOccurrences again from Developer > Macros.
Does the macro distinguish uppercase and lowercase letters?
Yes. The code uses vbBinaryCompare, so the phrase must match the capitalization in column H. To ignore case, replace vbBinaryCompare with vbTextCompare in the Replace function.
Can I count occurrences without a macro?
Yes. Enter =IF($I$1="","",(LEN(H2)-LEN(SUBSTITUTE(H2,$I$1,"")))/LEN($I$1)) in I2, then fill it down through I1000. This formula is case-sensitive and counts non-overlapping matches.
Why can't I save the macro in an XLSX file?
The XLSX format does not retain VBA code. Choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm) to keep the macro.
Will the macro count whole words only?
No. It counts the search phrase even when it appears inside a longer word. Whole-word matching requires a different approach that checks word boundaries.




