VBA & Macro Problems

Fix Word VBA Deleting Table Row When UserForm TextBox is Not Empty

Huda QurayshiHuda Qurayshi Oct 29, 2026 869 views

Question details

A Word VBA macro is incorrectly deleting a bookmarked table row even when the associated UserForm TextBox contains text.

Fix Word VBA Deleting Bookmarked Table Row When TextBox is Not Empty
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.
Before you start

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.

Solution 1Recommended

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.

1
Open the VBA Editor

Press Alt + F11 to open the Visual Basic for Applications (VBA) editor and locate your UserForm code.

2
Update the Conditional Logic

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.

3
Account for Blank Spaces

If users might accidentally enter spaces, wrap the value in a Trim function to prevent false positives: If Trim(Me.TextBoxx25.Value) <> vbNullString.

4
Verify the Bookmark

Ensure the bookmark "bmrow1" correctly spans the intended table row in your document and hasn't been accidentally overwritten.

Use an Explicit If...Else Statement with vbNullString
Best Practice: Always use vbNullString instead of "" in VBA. It is a built-in constant that requires zero memory allocation and processes faster.
Advanced VBA Support

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. 1. Install WPS Office: Download and install WPS Office on your computer.
  2. 2. Open Your Document: Open your macro-enabled document (.docm) in WPS Writer.
  3. 3. Access the Developer Tools: Navigate to the Developer tab and click on the VBA Editor icon to view your code.
  4. 4. Run Your Macro: Edit your If...Else logic if necessary, then run your UserForm code to execute the document automation seamlessly.
Built-in VBA editor for creating UserForms and debugging macrosFully compatible with Microsoft Word macro-enabled files (.docm)Familiar developer tools and seamless macro migrationFree, lightweight, and offers fast code execution performance
microsoft office alternative - wps office

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.