logo
search
VBA & Macro Problems

How to Stop a Find Loop at the Last Occurrence in Word VBA

Steve KSteve K Oct 10, 2026 870 views

Question details

The user needs to stop a Word VBA macro Find loop correctly after locating the final occurrence of a specific text format (e.g., font size 20) and changing the preceding character.

How to Stop a Find Loop at the Last Occurrence in Word VBA
Product
Word
Device & OS
not provided
Scenario
Running a VBA macro script to automate finding and modifying specific text formats throughout a document.
Observed behavior
The macro may enter an infinite loop at the end of the document, or trigger a 'Loop without Do' compile error if the code structure is incorrect.
Before you start

Ensure you have saved a backup copy of your document before running or testing new VBA macros to prevent accidental data modifications.

Solution 1Recommended

Use Document Range Tracking in a Do Loop

The most reliable way to prevent an infinite loop is to store the starting document range and exit the loop when the search fails or returns to the original range.

Relying solely on page numbers or the Selection.Find.Found property can cause infinite loops, especially if valid matches exist on the last page of the document. Tracking the document range ensures the macro accurately knows when it has processed the entire document.

1
Store the starting range

Define and store the starting document range at the beginning of your VBA script using a Range object variable.

2
Create a Do loop for searching

Set up a Do...Loop structure to search for the next occurrence of your target criteria, such as font size 20.

3
Perform the secondary search

Add a backward search within the loop to locate and modify the preceding character (e.g., changing the pipe character to black).

4
Define the exit condition

Include an If statement to exit the loop (Exit Do) when Selection.Find.Found evaluates to False or if the current search range intersects with the original starting range.

Use Document Range Tracking in a Do Loop
Avoid Page Number Logic: Do not rely on active end page numbers to terminate the loop, because valid matches may exist on the last page and will be skipped or cause the script to loop endlessly.
Run Macros Seamlessly in WPS

Execute and Debug VBA Macros in WPS Writer

WPS Office provides excellent support for VBA macros, allowing you to run, edit, and debug your document automation scripts with ease.

  1. 1. Open your document: Launch WPS Writer and open the document containing your target text.
  2. 2. Access the Developer tab: Navigate to the Developer tab on the top ribbon to access macro tools.
  3. 3. Open the VBA Editor: Click on 'Macros' or 'Visual Basic' to open the integrated VBA Editor.
  4. 4. Insert your macro code: Paste or edit your Find loop macro code in the correct module.
  5. 5. Run the macro: Execute the macro to automatically process your document formatting and stop at the last occurrence.
Highly compatible with Microsoft Word .docm and .dotm macro-enabled formats.Built-in VBA editor for writing and debugging automation scripts.Lightweight software with fast document loading and processing speeds.
microsoft office alternative - wps office

Frequently Asked Questions

Why does my Word VBA Find loop run endlessly?

An endless loop occurs when the search criteria loop back to the beginning of the document or get stuck on the last found item without a proper exit condition. Using range tracking ensures the loop exits once it passes the final occurrence.

What does the 'Loop without Do' compile error mean in VBA?

This error usually happens when an If statement inside a Do...Loop is not properly closed with an End If before the Loop command is reached, disrupting the code structure.

Can I use page numbers to stop a macro at the end of a document?

While you can track the active page using Selection.Information(wdActiveEndPageNumber), it is not recommended for stopping Find loops. Doing so might cause the macro to skip valid matches located on the final page.