VBA & Macro Problems

Count Phrase Occurrences in Excel Cells with VBA

Bushra ParveenBushra Parveen Nov 18, 2026 869 views

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.

Count Phrase Occurrences in Excel Cells with 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.
Before you start

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.

Solution 1Recommended

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.

1
Enter the search phrase

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.

2
Open the VBA editor

In desktop Excel, choose Developer > Visual Basic. On Windows, you can also press Alt+F11.

3
Insert the macro

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

4
Run the macro

Return to the worksheet you want to search, then choose Developer > Macros, select CountPhraseOccurrences, and click Run. Check the counts in I2:I1000.

5
Save the macro

To keep the VBA code in the workbook, choose File > Save As and select Excel Macro-Enabled Workbook (*.xlsm).

Run a VBA macro to count the phrase in each cell
How matches are counted: The macro counts the phrase wherever it appears, including inside a longer word; it does not restrict matches to whole words. If a source cell contains an Excel error, its result is #VALUE!.
Free Microsoft Office alternative

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.

Count non-overlapping phrase occurrences with spreadsheet formulasCompatible with Microsoft Excel formats such as XLSXFamiliar spreadsheet interface makes everyday editing easyFree and lightweight for opening and editing Office documents
microsoft office alternative - wps office

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.