How to Automatically Delete Word Sections and Rows with a Macro
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'.

- 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 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.
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.
Press Alt + F11 on your keyboard while in your Word document to open the Visual Basic for Applications (VBA) editor.
Click 'Insert' from the top menu and select 'Module' to create a blank workspace for your code.
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').
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.

Update Linked Fields Before Running the Macro
Ensure all linked Excel data is current before your macro searches for the trigger words to guarantee accurate deletions.
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. Enable Macros: Open WPS Writer, navigate to the 'Developer' tab, and ensure Macros are enabled for your document.
- 2. Open the VBA Editor: Click the 'Visual Basic' button or press Alt + F11 to launch the macro editing interface.
- 3. Add Your Script: Insert a new module and paste your custom VBA script designed to find and delete the specific rows.
- 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.

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.




