I have provided with 150 tables to use in a document. Most of the tables
have empty rows (to divide data visually). Due to space considerations, I'd
like to get rid of all the blank rows. Is there a way to find and delete only
the empty rows in a table?
MS Word version is 2003. Operating system is Windows XP Professional Version
2002 Service Pack 2.
Thanks!
Beth
---------------
Sub DeleteEmptyRows_AllTables()
Dim oTable As Table
Dim oRow As Row
For Each oTable In ActiveDocument.Tables
For Each oRow In oTable.Rows
'Check whether row is empty - delete if it is
If Len(oRow.Range.Text) = oRow.Cells.Count * 2 + 2 Then
oRow.Delete
End If
Next oRow
Next oTable
Exit Sub
End Sub
---------------
How it works:
The macro iterates through all tables in the active document and finds (and
deletes) the empty rows by checking the total string length of each row.
- An empty cell includes a cell maker with a length of 2.
- In addition, each row includes an end of row marker with a length of 2.
Therefore, the row is empty if the string length is equal to the number of
cells in the row multiplied by 2 + 2.
You will find another macro version at:
http://word.mvps.org/FAQs/MacrosVBA/DeleteEmptyRows.htm
NOTE that both macro versions will fail if the table contains vertically
merged cells (requires some error handling).
For help on installing macros, see:
http://www.gmayor.com/installing_macro.htm
--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
---
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
For Each oTable In ActiveDocument.Tables
with the following line:
For Each oTable In Selection.Sections(1).Range.Tables
Then the macro will only delete empty rows from the tables in the section in
which the insertion point is found (or in case you have selected text, in the
section where the selection starts).
--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word
Sub DeleteEmptyRows()
'
' DeleteEmptyRows Macro
'
'
Dim oTable As Table
Dim oRow As Row
For Each oTable In ActiveDocument.Tables
For Each oRow In oTable.Rows
'Check whether row is empty - delete if it is
If Len(oRow.Range.Text) = oRow.Cells.Count * 2 + 2 Then
oRow.Delete
End If
Next oRow
Next oTable
Exit Sub
End Sub
The issue is that once a mail merge is done and you have say 100 pages of
tables (each table seperated by a ":::::::::::section break (next
page)::::::::::"), and you run the macro above, ONLY the blank rows in the
FIRST Table/page are deleted. My tables have 6 columns and 21 rows. FYI -
if I move to the next table/page
Are the rows really empty – have you turned on non-printing characters
(Ctrl+Shift+8) to check that there are no spaces, tabs or other non-printing
characters?
Have you tried stepping through the macro using F8 in the VBE editor while
you watch what happens in your document?
--
Regards
Lene Fredborg
DocTools - Denmark
www.thedoctools.com
Document automation - add-ins, macros and templates for Microsoft Word