VBA & Macro Problems

How to Run Word Find and Replace Only in Selected Table Cells in WPS Writer VBA

Ayan MasoodAyan Masood Nov 13, 2026 869 views

Question details

The user needs a Word-compatible macro that performs multiple Find and Replace operations inside only the selected table cells, without losing the original table selection after the first replacement.

How to Run Word Find and Replace Only in Selected Table Cells
Product
WPS Writer
Device & OS
not provided
Scenario
Automating several find-and-replace passes in a selected table.
Observed behavior
Selection.Find can change or lose the original selection after the first replacement, so subsequent replacements may target the wrong range.
Before you start

Open the document in WPS Writer and make sure the Developer tab is visible so you can access the macro editor. Confirm that the cursor or selection is inside the table you want to process before running the macro.

Solution 1Recommended

Restrict Replacements Using Selection.Tables(1).Range in the Macro

Use this solution when the macro must perform multiple Find and Replace operations and stay limited to the table containing the current selection.

Selection.Find operates against the live selection and can lose the original table context after a replacement. By targeting Selection.Tables(1).Range, the macro uses the full table range as a stable object for repeated replacements.

1
Open the macro editor in WPS Writer

Click the Developer tab, then select Visual Basic or Macros to open the editor containing your Word-compatible macro code.

2
Check that the selection is inside a table

Add an early guard such as: If Selection.Information(wdWithInTable) = False Then Exit Sub. This prevents the macro from running when the cursor is not in a table.

3
Use the table range instead of Selection.Find

Replace Selection.Find with Selection.Tables(1).Range as the target, for example: Selection.Tables(1).Range.Find.Execute FindText:="1 (2–3)", ReplaceWith:="...".

4
Keep the same range for all replacements

Store Selection.Tables(1).Range in a Range variable once, then call the Find and Replace method on that variable for each replacement pair so the original table range is reused.

Restrict Replacements Using Selection.Tables(1).Range in the Macro
Replacements stay in the selected table: The macro processes only the table containing the selection and does not touch other tables or the rest of the document.
WPS Writer Macro Solution

Use WPS Writer Macros to Confine Find and Replace to a Selected Table

WPS Writer can run Word-compatible macros and gives you a free, lightweight environment to edit and execute the table-range Find and Replace code. By using Selection.Tables(1).Range in the macro, you keep every replacement within the selected table.

  1. 1. Open the macro editor: In WPS Writer, enable the Developer tab, click Developer > Macros, and create or edit the macro that contains your Find and Replace code.
  2. 2. Verify the table context: At the start of the macro, test Selection.Information(wdWithInTable). If it is False, exit the macro or show a prompt asking the user to place the cursor inside a table.
  3. 3. Target the selected table range: Use Selection.Tables(1).Range as the Find target and run the Find.Execute method on that range for each replacement.
  4. 4. Run the macro: Press F5 or click Run in the macro editor, then verify that the changes appear only in the selected table.
Keeps Find and Replace operations inside only the selected table instead of the whole documentMaintains compatibility with Microsoft Word .doc and .docm macro filesProvides a familiar macro/VBA editor interface in WPS WriterOffers a free and lightweight alternative for editing Word documents
microsoft office alternative - wps office

Frequently Asked Questions

Why does Selection.Find lose my original table selection after the first replacement?

Selection.Find changes the selection as soon as a replacement is made, so later code no longer refers to the original table. Use Selection.Tables(1).Range or store that range in a Range variable and run each replacement on that range.

How do I check whether the cursor is inside a table before running the macro?

Use Selection.Information(wdWithInTable). If it returns False, exit the macro or show a message; if True, continue with Selection.Tables(1).Range.

Will this method avoid replacing text outside the selected table?

Yes. The code uses the range of Selection.Tables(1), so it only processes the table containing the active selection and ignores body text and other tables.

Can I run multiple Find and Replace operations in the same selected table?

Yes. Assign Selection.Tables(1).Range to a Range variable once, then call Find.Execute on that variable for each search-and-replace pair so the range is not lost between operations.