PDA

View Full Version : [SOLVED:] Delete all empty rows in all tables



fbucaram
01-02-2018, 02:32 PM
Hi,

I am using the following macro to delete all rows in all tables of a word document:


Option Explicit

Public Sub DeleteEmptyRows()

Dim oTable As Table, oRow As Range, oCell As Cell, Counter As Long, _
NumRows As Long, TextInRow As Boolean

' Specify which table you want to work on.
For Each oTable In ActiveDocument.Tables
' Set a range variable to the first row's range
Set oRow = oTable.Rows(1).Range
NumRows = oTable.Rows.Count
Application.ScreenUpdating = False

For Counter = 1 To NumRows

StatusBar = "Row " & Counter
TextInRow = False

For Each oCell In oRow.Rows(1).Cells
If Len(oCell.Range.Text) > 2 Then
'end of cell marker is actually 2 characters
TextInRow = True
Exit For
End If
Next oCell

If TextInRow Then
Set oRow = oRow.Next(wdRow)
Else
oRow.Rows(1).Delete
End If

Next Counter
Next oTable
Application.ScreenUpdating = True

End Sub

It is working, however, all the columns that have empty rows end up opening very wide.

Can somebody please give me a hand?


21279

macropod
01-02-2018, 02:52 PM
Try:

Sub DeleteEmptyRowsFromAllTables()
Application.ScreenUpdating = False
Dim t As Long, r As Long
With ActiveDocument
For t = .Tables.Count To 1 Step -1
With .Tables(t)
.AllowAutoFit = False
For r = .Rows.Count To 1 Step -1
With .Rows(r)
If Len(.Range.Text) = .Cells.Count * 2 + 2 Then .Delete
End With
Next r
End With
Next t
End With
Application.ScreenUpdating = True
End Sub

fbucaram
01-02-2018, 03:30 PM
Thank you very much macropod! That works perfectly.

One more question. Is there any way the macro can be applied before a mail merge? So each table of all the different destinataries are sent without the empy rows?

macropod
01-02-2018, 03:51 PM
That suggests you're taking the wrong approach. Your mailmerge should be configured to produce only the rows that are needed. That can be done in one of three ways:
1. Use of a DATABASE field in the mailmerge main document;
2. Use of a Directory/Catalog merge; or
3. Use of a many-to-one addin.

For 1. see:
http://answers.microsoft.com/en-us/office/forum/office_2010-word/many-to-one-email-merge-using-tables/8bce1798-fbe8-41f9-a121-1996c14dca5d
and:
https://answers.microsoft.com/en-us/msoffice/forum/msoffice_word-mso_winother-mso_2010/mail-merge-to-a-word-table-on-a-single-page/4edb4654-27e0-47d2-bd5f-8642e46fa103


For 2. see:

http://windowssecrets.com/forums/showthread.php/154370-Microsoft-Word-Catalogue-Directory-Mailmerge-Tutorial
or:
http://www.gmayor.com/Zips/Catalogue%20Mailmerge.zip


For 3. see:
http://www.gmayor.com/ManyToOne.htm
and:
http://bit.ly/1hduSCB

gmayor
01-02-2018, 10:27 PM
Duplicate Post at http://www.msofficeforums.com/word-vba/37765-delete-all-empty-rows-all-tables.html - Grrrrr!:banghead:

macropod
01-02-2018, 10:36 PM
fbucaram: Please read VBA Express' policy on Cross-Posting in item 3 of the rules: http://www.vbaexpress.com/forum/faq.php?faq=new_faq_item#faq_new_faq_item3

fbucaram
01-03-2018, 11:42 AM
:doh::friends:

macropod
01-03-2018, 03:35 PM
Also cross-posted at: https://stackoverflow.com/questions/48068182/word-vba-delete-all-empty-rows-from-all-tables
Kindly provide the cross-post links between *all* of the forums you've done this on.