Document Editing Problems

How to Delete a Word Table Row When a VBA Text Box Is Empty

Bushra ParveenBushra Parveen Oct 29, 2026 869 views

Question details

The user wants to use Word VBA to evaluate if a specific UserForm text box is empty and automatically delete the corresponding table row in the document if it has no value.

How to Delete a Word Table Row When a VBA Text Box Is Empty
Product
Microsoft Word
Device & OS
not provided
Scenario
Transferring data from a VBA UserForm into a Word document.
Observed behavior
The goal is to seamlessly remove a related table row when the specified text box has no value submitted during the UserForm execution.
Before you start

Ensure you have saved a backup of your Word document before running VBA macros, and verify that the Developer tab is enabled to access the Visual Basic Editor.

Solution 1Recommended

Use a Conditional VBA Statement to Delete the Table Row

Add an If statement in your UserForm code to check for an empty text box value and delete the mapped table row using the Rows.Delete method.

To accomplish this without requiring additional plugins, you can directly check the value of the text box when the user submits the form. If the string is empty, the macro locates the specific row in your document's table and deletes it.

1
Open the Visual Basic Editor

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

2
Access the UserForm Code

Locate your UserForm in the Project Explorer panel, right-click it, and select 'View Code'.

3
Add the Conditional Logic

Inside your submit button's click event, add an 'If' statement to check the text box. For example: If TextBox1.Value = "" Then.

4
Execute the Row Deletion

Within the 'If' block, reference the target table and row to delete it. For example: ActiveDocument.Tables(1).Rows(2).Delete.

5
Test the Macro

Run the UserForm to test the logic, ensuring the row correctly disappears when the text box is left blank.

Use a Conditional VBA Statement to Delete the Table Row
Use Meaningful Names: Always assign meaningful names to your UserForm controls (e.g., txtStartDate instead of TextBox25) to make your VBA code easier to read and maintain.
Free Microsoft Office alternative

Try WPS Office for Seamless Word Document Editing

While complex VBA UserForms and ActiveX controls are native to Microsoft Word, WPS Office provides a lightweight, highly compatible, and free alternative for all your everyday document editing needs without the hassle of configuring macros.

  1. 1. Download the Software: Visit the official WPS Office website and click on the Free Download button.
  2. 2. Install WPS Office: Run the downloaded installation file and follow the on-screen prompts to set up the suite on your device.
  3. 3. Edit Your Documents: Open your .docx file in WPS Writer to manage and format your document tables easily without relying on complex macros.
Fully compatible with Microsoft Word (.docx) formats.Lightweight document editing alternative to complex VBA configurations.Familiar user interface for a seamless transition.Cross-platform support across Windows, Mac, and Linux.
microsoft office alternative - wps office

Frequently Asked Questions

Can I undo a table row deletion triggered by VBA?

Yes, you can typically use the standard Undo function (Ctrl + Z) in Word immediately after the macro executes, provided the macro execution has not cleared the undo stack.

Why is my VBA code not deleting the correct table row?

This usually happens if the table or row index is incorrect. Ensure you are referencing the correct table number (e.g., Tables(1) for the first table in the document) and that the row index matches the exact row you wish to remove.

Do I need to enable macros to use UserForms in Word?

Yes, to run VBA code and display UserForms, your document must be saved as a Macro-Enabled Document (.docm), and macros must be explicitly enabled in your Word Trust Center settings.

What is the simplest VBA code to check if a text box is empty?

You can use a straightforward If statement like `If TextBox1.Value = "" Then` or `If Len(TextBox1.Value) = 0 Then` to verify if the text box contains no text.