VBA & Macro Problems

Replace Specific Word Occurrences in Each Sentence with a Word VBA Macro

Phi Hung VoPhi Hung Vo Nov 14, 2026 868 views

Question details

Replace the second, third, and fourth occurrences of a specified token in each sentence without editing hundreds of sentences manually.

Replace Specific Word Occurrences in Each Sentence with a Word VBA Macro
Product
Microsoft Word
Device & OS
not provided
Scenario
A document contains approximately 500 sentences that need occurrence-specific replacements.
Observed behavior
Each qualifying sentence should retain the first occurrence and replace the next three occurrences with specified values.
Before you start

Identify the exact token and the three replacement values, and test them on a few representative sentences. In Word, use File > Save As to create a working copy so you can compare it with the original.

Solution 1Recommended

Run a Word VBA macro to replace occurrences in each sentence

Use this macro when each qualifying sentence contains at least four occurrences of the same whole-word token.

The macro uses Word's sentence boundaries, matches whole words without regard to case, and skips sentences with fewer than four matches. It replaces matches from the fourth back to the second so earlier match positions are preserved.

Replace the example constants with your actual token and values. Avoid sentence-ending punctuation in the replacement values, and check how Word identifies sentences in your document before processing the full file.

1
Open the VBA editor

Open the working copy in Microsoft Word, then open Developer > Visual Basic. If Developer is hidden, enable it through Word's ribbon settings.

2
Add the macro

In the VBA editor, select Insert > Module and paste this code: Sub ReplaceSentenceOccurrences() Const TARGET As String = "TOKEN" Const REPLACE_2 As String = "SECOND" Const REPLACE_3 As String = "THIRD" Const REPLACE_4 As String = "FOURTH" Dim doc As Document Dim sentenceRange As Range, searchRange As Range Dim hits(1 To 4) As Range Dim i As Long, n As Long, foundCount As Long Dim changed As Long, skipped As Long Set doc = ActiveDocument For i = doc.Sentences.Count To 1 Step -1 Set sentenceRange = doc.Sentences(i).Duplicate Set searchRange = sentenceRange.Duplicate foundCount = 0 For n = 1 To 4 With searchRange.Find .ClearFormatting .Text = TARGET .Forward = True .Wrap = wdFindStop .Format = False .MatchCase = False .MatchWholeWord = True .MatchWildcards = False .MatchSoundsLike = False .MatchAllWordForms = False If Not .Execute Then Exit For End With Set hits(n) = searchRange.Duplicate foundCount = n searchRange.SetRange Start:=searchRange.End, End:=sentenceRange.End Next n If foundCount = 4 Then hits(4).Text = REPLACE_4 hits(3).Text = REPLACE_3 hits(2).Text = REPLACE_2 changed = changed + 1 Else skipped = skipped + 1 End If Next i MsgBox changed & " sentences changed; " & skipped & " sentences skipped." End Sub

3
Set the replacement values

In the module, change TOKEN to the target word and change SECOND, THIRD, and FOURTH to the values required for those occurrences. Keep the quotation marks around each value.

4
Run and save

Return to the working document, select Developer > Macros, choose ReplaceSentenceOccurrences, and click Run. Check the changed and skipped counts, inspect sample sentences, and save the revised document.

5
Compare the documents

Select Review > Compare > Compare. Choose the original and revised documents, run the comparison, and inspect the changes to confirm the intended occurrences were replaced.

Run a Word VBA macro to replace occurrences in each sentence
Check sentence and token matching: Word's sentence boundaries may differ from your intended boundaries, especially around abbreviations. The macro replaces only the second through fourth whole-word matches; it leaves the first and any later matches unchanged.
Free Microsoft Office alternative

Edit Word documents with WPS Office

WPS Office is a free, lightweight alternative with a familiar interface for opening and editing Word documents. Its format compatibility helps you move existing files into your workflow, although it does not fix or guarantee execution of Microsoft Word VBA macros.

Open and edit Microsoft Word DOC and DOCX filesReview and edit sentences in a familiar word-processing interfaceMove existing documents into a lightweight office suiteUse a free alternative for everyday document editing
microsoft office alternative - wps office

Frequently Asked Questions

What happens if a sentence has fewer than four matching words?

The macro skips that sentence without changing it and includes it in the skipped count shown after the run.

Does the macro replace words inside longer words?

No. The Find setting MatchWholeWord is True, so the target must be a whole-word match. Change the target constant in the VBA module to the word you need.

How can I check the replacements against the original?

In Microsoft Word, select Review > Compare > Compare, choose the original and revised files, and inspect the resulting comparison.

Will the macro change the fifth occurrence or later ones?

No. It finds the first four matches in each sentence and replaces only matches two, three, and four.

Why might Word split a sentence differently than I expect?

Word determines sentence boundaries using its own rules, which can be affected by punctuation and abbreviations. Test the macro on representative text and inspect the results before running it across the full document.