Fix Word VBA Deleting Table Row When UserForm TextBox is Not Empty
Question details
A Word VBA macro is incorrectly deleting a bookmarked table row even when the associated UserForm TextBox contains text.

- Product
- Word
- Device & OS
- not provided
- Scenario
- Running a VBA UserForm to evaluate user input and conditionally delete a specific table row based on the textbox value.
- Observed behavior
- The table row is being deleted regardless of whether the UserForm TextBox is completely empty or actually contains text.
Ensure you have saved a backup copy of your Word document before modifying your VBA code, and verify that the bookmark name in your document matches the one in your script exactly.
Use an Explicit If...Else Statement with vbNullString
Update your VBA code to explicitly check if the TextBox value equals vbNullString before executing the row deletion command.
Implicit conditions or missing Else blocks often cause VBA to execute the deletion command regardless of the TextBox contents. Testing against vbNullString explicitly ensures the macro only deletes the row when the input field is genuinely blank.
Press Alt + F11 to open the Visual Basic for Applications (VBA) editor and locate your UserForm code.
Replace your existing condition with a complete If...Else structure. For example: If Me.TextBoxx25.Value <> vbNullString Then Debug.Print Me.TextBoxx25.Value Else Doc1.Bookmarks("bmrow1").Range.Cells.Delete End If.
If users might accidentally enter spaces, wrap the value in a Trim function to prevent false positives: If Trim(Me.TextBoxx25.Value) <> vbNullString.
Ensure the bookmark "bmrow1" correctly spans the intended table row in your document and hasn't been accidentally overwritten.

Run VBA Macros Seamlessly in WPS Writer
WPS Office provides built-in VBA support, allowing you to write, edit, and debug UserForm macros just as you would in Microsoft Word. Enjoy full control over document automation and table manipulation.
- 1. Install WPS Office: Download and install WPS Office on your computer.
- 2. Open Your Document: Open your macro-enabled document (.docm) in WPS Writer.
- 3. Access the Developer Tools: Navigate to the Developer tab and click on the VBA Editor icon to view your code.
- 4. Run Your Macro: Edit your If...Else logic if necessary, then run your UserForm code to execute the document automation seamlessly.

Frequently Asked Questions
Why does my VBA code delete the row even when the textbox appears empty?
If your textbox contains invisible characters like spaces, VBA will not read it as completely empty. You should use Trim(Me.TextBoxx25.Value) = vbNullString to ensure any accidental whitespace is ignored before evaluating the condition.
What is the difference between vbNullString and "" in Word VBA?
Both represent an empty string, but vbNullString is a built-in constant that requires zero memory allocation. It is considered the best practice for checking empty variables and textboxes in VBA.
How do I properly bookmark a table row to use in my VBA macro?
Select the entire row in your document, navigate to Insert > Bookmark on the ribbon, type a name without spaces (e.g., bmrow1), and click Add. Your VBA code can now reference this specific row using the Bookmarks collection.
How can I debug if my macro correctly reads the UserForm TextBox value?
Use Debug.Print Me.TextBoxx25.Value or MsgBox Me.TextBoxx25.Value immediately before your If statement. This prints the exact value to the Immediate Window or displays a pop-up, helping you verify what the macro is actually processing.




