VBA & Macro Problems

How to Use Word VBA Macro to Delete Lines Between Text Sections

Olivia MillerOlivia Miller Nov 2, 2026 869 views

Question details

The user needs a VBA macro to delete variable pasted lines between repeating text sections while keeping the first and last lines intact.

How to Delete Lines Between Text Sections Using a Word VBA Macro
Product
Word
Device & OS
not provided
Scenario
Cleaning up a document by removing unwanted lines of pasted text situated between specific recurring sections.
Observed behavior
Variable lines need to be removed automatically based on a consistent text marker, specifically the phrase 'Full Text'.
Before you start

Always create a backup copy of your document before running any new VBA macros, as macro actions clear the undo history and cannot be reverted.

Solution 1Recommended

Create and Run a Search-and-Delete VBA Macro

Write a custom VBA macro that locates your specific keyword marker and deletes the targeted paragraphs below it.

To accomplish this, you need a macro that searches for the 'Full Text' string, manipulates the selection by moving down a specified number of paragraphs, and deletes the unwanted range. The exact paragraph count depends on your document's line spacing and structure.

1
Open the VBA Editor

Open your Word document and press Alt + F11 on your keyboard to launch the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

In the VBA editor window, click 'Insert' from the top menu bar and select 'Module' to create a blank script window.

3
Write the Macro Code

Paste or write a VBA script utilizing the Selection.Find.Execute method to search for 'Full Text'. Use Selection.MoveDown Unit:=wdParagraph to skip the lines you want to keep, and Selection.Delete to remove the variable pasted lines.

4
Execute the Macro

Press F5 or click the green 'Run' button in the toolbar to execute the macro. The script will iterate through the document and clean up the sections.

Create and Run a Search-and-Delete VBA Macro
Test Before Execution: Test the macro on a small sample document first to ensure your paragraph counting logic aligns perfectly with the document's structure before applying it to the full file.
Efficient Document Editing

Automate Tasks with Macros in WPS Writer

WPS Office offers robust word processing capabilities and supports standard VBA macros. You can easily automate repetitive tasks, such as deleting specific lines across large documents, within a clean and familiar interface.

  1. 1. Install WPS Office: Download and install WPS Office, ensuring you select the version that includes the VBA module for macro support.
  2. 2. Open Your Document: Launch WPS Writer and open the document you wish to clean up.
  3. 3. Access the Developer Tools: Navigate to the Developer tab on the ribbon and click on 'Macros', or press Alt + F11 to open the VBA editor.
  4. 4. Run Your Script: Paste your custom macro code into the module and click 'Run' to instantly delete the unwanted text sections.
Supports standard Word VBA scripts for advanced document automationSeamlessly compatible with Microsoft Word .doc and .docx formatsLightweight, fast, and completely free to download
microsoft office alternative - wps office

Frequently Asked Questions

Can I undo a VBA macro after it finishes running?

No, actions performed by a VBA macro typically clear the undo history in Word. You cannot press Ctrl+Z to revert the changes. Always save a duplicate of your document before executing a new macro.

How do I show the Developer tab to access VBA?

Go to File > Options > Customize Ribbon. In the right-hand column, check the box next to 'Developer' and click OK. The Developer tab will now appear on your main ribbon.

Why is my macro skipping some 'Full Text' markers?

This usually happens if the search string doesn't exactly match the text in the document. Check for trailing spaces, hidden formatting, or case sensitivity issues. Ensure your VBA code sets Find.MatchCase and Find.MatchWholeWord appropriately.

Does WPS Office support VBA macros?

Yes, WPS Office supports VBA macros. However, depending on your region and the specific edition of WPS Office you downloaded, you may need to install the VBA module or an add-in separately to enable the Developer tab.