How to Delete a Word Table Row When a VBA Text Box Is Empty
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.

- 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.
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.
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.
Open your Word document and press ALT + F11 to launch the Visual Basic for Applications (VBA) editor.
Locate your UserForm in the Project Explorer panel, right-click it, and select 'View Code'.
Inside your submit button's click event, add an 'If' statement to check the text box. For example: If TextBox1.Value = "" Then.
Within the 'If' block, reference the target table and row to delete it. For example: ActiveDocument.Tables(1).Rows(2).Delete.
Run the UserForm to test the logic, ensuring the row correctly disappears when the text box is left blank.

Implement a DatePicker Control to Prevent Invalid Entries
If the text box expects a date, use a calendar or DatePicker control so users are forced to input valid data or explicitly leave it empty.
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. Download the Software: Visit the official WPS Office website and click on the Free Download button.
- 2. Install WPS Office: Run the downloaded installation file and follow the on-screen prompts to set up the suite on your device.
- 3. Edit Your Documents: Open your .docx file in WPS Writer to manage and format your document tables easily without relying on complex macros.

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.




