PDA

View Full Version : [SOLVED:] Delete blank rows in Word table



DavG63
04-21-2017, 06:26 AM
Hi

I'm hoping someone will be able to give me a quick answer for this as I don't think the problem is that complicated - I just can't see the solution.

I have several iterations of the same table in a Word document (repeated on several different letters).
















Each cell in the first column contains a single bullet point - that's it. Nothing else.
Each cell in the second column contains information by way of DocVariables which may or may not display a value.

I have code from a previous document that deletes out the rows in a table if they are blank.



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



It isn't working this time however and I suspect it's because of the bulletpoint.

Is there a way to tweak my code to ignore the initial column and delete the row if the cell in the second column is blank instead?

Any help would be great.

Thanks

Dav

gmayor
04-21-2017, 06:52 AM
How about


Sub DeleteEmptyRows()
Dim Tbl As Table, cel As Cell
Dim i As Long, j As Long, n As Long, fEmpty As Boolean
Application.ScreenUpdating = False
With ActiveDocument
For Each Tbl In .Tables
n = Tbl.Rows.Count
For i = n To 1 Step -1
fEmpty = True
For j = 2 To Tbl.Rows(i).Cells.Count
Set cel = Tbl.Rows(i).Cells(j)
If Len(cel.Range.Text) > 2 Then
fEmpty = False
Exit For
End If
Next j
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

DavG63
04-21-2017, 08:38 AM
Hi Graham

Thanks for your suggestion. I've run this through and I'm afraid it doesn't appear to delete out the rows, I'm still left with either one or two rows with a bulletpoint and no information beside it.

Any other ideas?

Thanks

Dav

gmayor
04-21-2017, 08:28 PM
It should work, and does here if the remaining cells in the row are all empty - a space in a cell will cause it to ignore the row. Click the ¶ button on the Home tab to display the formatting or if you cannot find the cause post a copy of the table that doesn't work as a document attachment.

gmaxey
04-22-2017, 06:13 AM
Try modifying this line:
If Len(cel.Range.Text) > 2 Then
to:
If Len(Trim(cel.Range.Text)) > 2 Then

gmayor
04-22-2017, 06:49 AM
That should work for a space, but it won't for (say) a non-breaking space or a tab character. Without knowing what is in the cell, it is difficult to come up with a solution.

gmaxey
04-22-2017, 07:19 AM
Graham,

True but just a bit bored and looking for something to do ;-).

Perhaps:


Sub DeleteEmptyRows()
Dim Tbl As Table, cel As Cell
Dim i As Long, j As Long, n As Long, fEmpty As Boolean
Application.ScreenUpdating = False
With ActiveDocument
For Each Tbl In .Tables
n = Tbl.Rows.Count
For i = n To 1 Step -1
fEmpty = True
For j = 2 To Tbl.Rows(i).Cells.Count
Set cel = Tbl.Rows(i).Cells(j)
If Not fcnJustWhiteSpace(cel) Then
fEmpty = False
Exit For
End If
Next j
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
Private Function fcnJustWhiteSpace(cel) As Boolean
Dim oChr
fcnJustWhiteSpace = True
For Each oChr In cel.Range.Characters
Select Case Asc(oChr)
Case 9, 11, 13, 32, 160
Case Else: fcnJustWhiteSpace = False: Exit For
End Select
Next
End Function

DavG63
04-24-2017, 05:13 AM
Hi Greg/Graham

Thanks very much for the suggestion - changing the code to
If Len(Trim(cel.Range.Text)) > 2 Then worked a charm.

I'll mark this as solved and thanks again.

Dav

gmaxey
04-24-2017, 01:44 PM
Good. Worked because some of the cells contained one or more spaces. If you try the other version it should take care of that plus any other "white space" i.e, tabs, linebreaks, etc.