VBA & Macro Problems

How to Automatically Delete Word Sections and Rows with a Macro

John WilsonJohn Wilson Nov 3, 2026 869 views

Question details

The user wants to automate the deletion of specific document sections or table rows using a macro when linked Excel data updates to display trigger phrases like 'Delete Section' or 'Delete Row'.

Automatically Delete Word Sections and Rows with a Macro
Product
Microsoft Word
Device & OS
not provided
Scenario
Updating a Word document linked to Excel data, where specific text updates should structurally remove document elements.
Observed behavior
Pressing F9 updates the linked fields to show the text, but the structural section or row remains visible instead of being automatically deleted.
Before you start

Before creating and running a VBA macro that deletes content, clearly define your document's exact structure and deletion rules, and ensure you have a backup copy of your file to prevent accidental data loss.

Solution 1Recommended

Create a VBA Macro to Delete Rows or Sections Based on Text

Design a custom VBA script that scans your document's tables or sections and deletes any object containing the specified trigger text.

Since simply updating linked fields (pressing F9) does not remove structural elements, a macro is required to search for the resulting text and execute a deletion command.

1
Open the VBA Editor

Press Alt + F11 on your keyboard while in your Word document to open the Visual Basic for Applications (VBA) editor.

2
Insert a New Module

Click 'Insert' from the top menu and select 'Module' to create a blank workspace for your code.

3
Write the Deletion Loop

Write a VBA script that iterates through ActiveDocument.Tables or ActiveDocument.Sections. Use an If statement to check if the range's text includes your specific trigger phrase (e.g., 'Delete Row').

4
Execute the Deletion Command

Within the If statement, add the command to delete the object (e.g., Row.Delete or Section.Range.Delete). Press F5 to run the macro.

Create a VBA Macro to Delete Rows or Sections Based on Text
Provide Clear Examples: If you are asking for exact code on a forum, always provide a representative example of your document's structure, as VBA code needs exact references to work properly.
Advanced VBA Support in WPS Office

Use WPS Writer to Automate Document Formatting with Macros

WPS Office provides robust support for VBA macros, allowing you to easily automate repetitive tasks like deleting specific sections or rows based on linked spreadsheet data.

  1. 1. Enable Macros: Open WPS Writer, navigate to the 'Developer' tab, and ensure Macros are enabled for your document.
  2. 2. Open the VBA Editor: Click the 'Visual Basic' button or press Alt + F11 to launch the macro editing interface.
  3. 3. Add Your Script: Insert a new module and paste your custom VBA script designed to find and delete the specific rows.
  4. 4. Run the Automation: Execute the macro directly from the editor or assign it to a quick access button to instantly clean up your document.
Fully compatible with Microsoft Word macro scriptsBuilt-in VBA editor for seamless coding and debuggingHighly compatible with Microsoft Office docx and docm formatsLightweight application with high performance
microsoft office alternative - wps office

Frequently Asked Questions

Can a linked Excel field delete a Word row on its own?

No. Linked fields can only update the text value displayed in the document. To perform structural changes like deleting a row or section, you must use a VBA macro.

How do I delete a specific section using VBA?

You can use the ActiveDocument.Sections collection in VBA. Write a loop to check the text within each Section.Range, and if your trigger word is found, use Section.Range.Delete to remove it.

Why isn't my macro finding the linked text?

Linked field text might contain hidden characters or paragraph marks. Ensure your macro searches for a partial string match (e.g., using InStr) or strips out trailing paragraph marks before comparing the text.

Does pressing F9 automatically run macros?

No, pressing F9 only updates the field codes (like linked Excel data or table of contents). Macros must be triggered separately via the Developer tab, a keyboard shortcut, or an assigned button.