Word VBA, Delete Empty Rows From Tables

One of the things a lot of word users encounter while working in word, are empty rows in tables. In this article I will provide a code that will delete all empty rows from all tables in the document.


Delete Empty Rows From Single Table:

In this section I will provide a code that will delete all empty rows from a single table. The code loops through the rows of the first table. It checks the first cell of each row. If the cell is empty that row will be deleted:

Sub Example1()
'loops through the rows in the table
Dim i As Integer
'gets the string in the first column of the table
Dim strString As String
'the number of deleted rows
Dim intDeleted As Integer
intDeleted = 0
'loops through the table rows
For i = 1 To ThisDocument.Tables.Item(1).Rows.Count
'gets the string in the first cell
strString = ThisDocument.Tables.Item(1).Rows.Item( _
i - intDeleted).Cells(1).Range
'checks if the cell is empty
If Strings.Len(strString) = 2 Then
'deletes the row
ThisDocument.Tables.Item(1).Rows.Item(i _
- intDeleted).Delete
intDeleted = intDeleted + 1
End If
Next i
End Sub

The for i loop iterates through the rows of the table:

For i = 1 To ThisDocument.Tables.Item(1).Rows.Count
...
Next i

The line below gets the text string in the first cell of the row:

strString = ThisDocument.Tables.Item(1).Rows.Item(i - intDeleted).Cells(1).Range

The If statement below checks if the cell is empty:

If Strings.Len(strString) = 2 Then
...
End If

It might be a little confusing. Why are we checking the size of the string to see if its equal to 2? Shouldn’t we check if the size of the string is “0”? In order to answer this question we would have to take a look at the watch window in the VBA editor at that line:
Word, VBA, Delete Empty Rows
As you can see although the cell is empty, but 2 characters have been assigned to the string variable. These 2 characters always exist in a table cell.

Before:
Word, VBA, Delete Empty Rows Before
After:
Word, VBA, Delete Empty Rows After


Delete Empty Rows From All Tables in the Document:

In this section the code in the previous section will be extended to delete the empty row from all the tables in the word document:

Sub Example2()
'loops through the rows in the table
Dim i As Integer
'gets the string in the first column of the table
Dim strString As String
'the number of deleted rows
Dim intDeleted As Integer
'loops throug the tables in the document
Dim j As Integer
'number of deleted table
Dim k As Integer

k = 0
For j = 1 To ThisDocument.Tables.Count
intDeleted = 0
'loops through the table rows
For i = 1 To ThisDocument.Tables.Item(j - k).Rows.Count
'gets the string in the first cell
strString = ThisDocument.Tables.Item(j - k).Rows.Item( _
i - intDeleted).Cells(1).Range
'checks if the cell is empty
If Strings.Len(strString) = 2 Then
If ThisDocument.Tables.Item(j - k).Rows.Count > 1 Then
'deletes the row
ThisDocument.Tables.Item(j - k).Rows.Item(i _
- intDeleted).Delete
intDeleted = intDeleted + 1
Else
'deletes the row
ThisDocument.Tables.Item(j - k).Rows.Item(i _
- intDeleted).Delete
intDeleted = intDeleted + 1
k = k + 1
End If
End If
Next i
Next j
End Sub

Before:
Word VBA, Delete All Before
After:
Word VBA, Delete All After

You can download the file and code related to this article from the link below:

See also:

One thought on “Word VBA, Delete Empty Rows From Tables”

  1. Oz says:

    Hi,

    This is very useful. Thank you very much. I was wondering if it is possible modify the code to check if cell in the 3rd column and 2nd row of the table is empty and delete.

    Thank you.

Leave a Reply

Your email address will not be published. Required fields are marked *