PDA

View Full Version : Tables - Deleting blank rows



hubs
09-03-2016, 10:25 AM
Hi,

I have got a Word document with several tables, and in between each table is text.

I need to delete the entire row only in tables that are blank or have no text.

Any help would be very much appreciated?

gmaxey
09-03-2016, 11:46 AM
hubs

You have code (from another post) that loops through document tables. This is not a free code writing service. Sometimes you will be handed the fish, but the idea is that you learn to catch your own fish.

What have you tried?

gmaxey
09-03-2016, 11:56 AM
Hint 1. When looping through things (rows) where you may delete 1 or more of those things and affect the count, start at the end and work back to 1:

For lngRow = oTbl.Rows.Count To 1 Step -1

Hint 2. An empty row will have a text length equal to: (1 + number of cells) * 2.

hubs
09-03-2016, 12:48 PM
Hint 1. When looping through things (rows) where you may delete 1 or more of those things and affect the count, start at the end and work back to 1:

For lngRow = oTbl.Rows.Count To 1 Step -1

Hint 2. An empty row will have a text length equal to: (1 + number of cells) * 2.

you are killing me haha

hubs
09-03-2016, 01:23 PM
you are killing me haha

The following code does the trick (code is plagiarised) but the method seems less efficient than yours :)


Sub DeleteEmptyTableRows()Application.ScreenUpdating = False
Dim Tbl As Table, cel As Cell, i As Long, n As Long, fEmpty As Boolean
With ActiveDocument
For Each Tbl In .Tables
n = Tbl.Rows.Count
For i = n To 1 Step -1
fEmpty = True
For Each cel In Tbl.Rows(i).Cells
If Len(cel.Range.Text) > 2 Then
fEmpty = False
Exit For
End If
Next cel
If fEmpty = True Then Tbl.Rows(i).Delete
Next i
Next Tbl
End With
Set cel = Nothing: Set Tbl = Nothing
Application.ScreenUpdating = True End Sub

gmaxey
09-04-2016, 04:49 PM
There you go. Something like this was what I had in mind:


Sub ScratchMacro()
'A basic Word macro coded by Greg Maxey
Dim oTbl As Table, cel As Cell, lngIndex As Long, n As Long, fEmpty As Boolean
With ActiveDocument
For Each oTbl In .Tables
For lngIndex = oTbl.Rows.Count To 1 Step -1
If Len(oTbl.Rows(lngIndex).Range.Text) = (oTbl.Rows(lngIndex).Cells.Count * 2) + 2 Then _
oTbl.Rows(lngIndex).Delete
Next lngIndex
Next oTbl
End With
lbl_Exit:
Exit Sub
End Sub