Delete a table empty rows in word document using VBA

TG2812

Board Regular
Joined
Apr 15, 2015
Messages
180
Hi,

I'm struggling with the following issues. I've got a table in word and depending if the whole row is empty or not, I'm trying to delete the empty rows.
I use the following VBA code but it doesn't work. Here I mean that no matter if the rows is empty or not, Excel will delete the given row. Here's the code I'm using:

i = report.Tables(1).Rows.Count
For n = i To 2 Step -1
If IsEmpty(report.Tables(1).Rows(i)) = False Then
report.Tables(1).Rows(i).Delete
i = i - 1
End If
Next

Does anyone has ever come across that issue?
 

Excel Facts

Can a formula spear through sheets?
Use =SUM(January:December!E7) to sum E7 on all of the sheets from January through December
Shouldn't this line be set to TRUE
Code:
If IsEmpty(report.Tables(1).Rows(i)) = False
 
Upvote 0
Firstly, the row may contain more than one column. So the IsEmpty function isn't really appropriate. Secondly, even if the row contained only one column, IsEmpty would always evaluate to False. That's because each cell contains "end of cell" markers consisting of two characters. So, in order to test whether a cell contains text, the Len function can be used to check whether the number of characters exceed two, as per the example included in the following link...

Deleting all empty rows in a table using a macro
 
Upvote 0
Thanks a ton everyone for your answers. I looked up into the link above. I came up with the following code but it still doesn't work. Sorry if it takes time for me to understand but I'm a beginner in VBA.
Here's the code:

Dim oTable As Table
Dim oRow As Range
Dim oCell As Cell
Dim NumRows As Long

Set oTable = Report.Tables(1)
Set oRow = oTable.Rows(i).Range
NumRows = oTable.Rows.Count


For i = 1 To NumRows
For Each oCell In oRow.Rows(i).Cell
If Len(oCell.Range.Text) = 0 Then
oRow.Rows(i).Delete
i = i - 1
End If
Next
Next

Any thoughts where the error could be?
 
Upvote 0
Try...

Code:
Dim Report As Document
Dim oTable As Table
Dim oRow As Row
Dim oCell As Cell
Dim NumRows As Long
Dim i As Long
Dim bTextExists As Boolean

Set Report = ActiveDocument 'assign the desired Word document accordingly

Set oTable = Report.Tables(1)

NumRows = oTable.Rows.Count

With oTable
    For i = NumRows To 1 Step -1
        Set oRow = .Rows(i)
        For Each oCell In oRow.Cells
            If Len(oCell.Range.Text) > 2 Then 'count characters, but exclude end of cell markers
                bTextExists = True
                Exit For
            End If
        Next
        If bTextExists Then
            bTextExists = False
        Else
            oRow.Delete
        End If
    Next
End With

Hope this helps!
 
Upvote 0
@Domenic Thanks a lot! It helped me so much. Besides, I've made slight changes to the loop:

With oTable
For i = NumRows To 1 Step -1
Set oRow = .Rows(i)
For Each oCell In oRow.Cells
If Len(oCell.Range.Text) > 2 Then 'count characters, but exclude end of cell markers
bTextExists = True
Exit For
End If
If bTextExists = False Then
oRow.Delete
End If
Next
Next
End With
 
Upvote 0
I guess that would work as long as all of the empty rows occur at the end of the table...
 
Upvote 0

Forum statistics

Threads
1,214,915
Messages
6,122,212
Members
449,074
Latest member
cancansova

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top