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?
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?
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?
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.
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
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