Word macro to delete identical rows in a doc

Closed
dmr316 Posts 4 Registration date Thursday July 23, 2009 Status Member Last seen July 24, 2009 - Jul 24, 2009 at 05:47 AM
 VTTP - Mar 1, 2010 at 11:40 AM
Hello everyone, I hope someone is able to help me. Every day I work with an RTF export from non-Microsoft software, which is a Word-like document set out with multiple tables in the style of 2 "newspaper columns" per page. I would open this file in Word 2003.

Unfortunately, a lot of the table rows are redundant (see below description) and it would be really convenient if there was a quick way to delete them all, seeing as these RTF exports can go on for about a hundred pages (which is why I decided not to delete them all by hand).

The rows that need to be deleted are of the form: Blank cell, Blank cell, Blank cell with a tab in it, Cell containing three characters: a space, then a zero, then another space. i.e. with the backwards-P symbol on it's [][][tab arrow][sp 0 sp]

On my first try at macros, I was able to delete just one row like that. However, it won't delete any other rows like that. And finally, because I've got multiple tables all with the same problem, I would dearly like to be able to delete those rows in other, separate, tables in that same document.

Thank you in advance for any help.

2 responses

dmr316 Posts 4 Registration date Thursday July 23, 2009 Status Member Last seen July 24, 2009
Jul 24, 2009 at 10:07 AM
I have found some macros on the internet which may help:

This macro finds each table in turn:
Sub FindTables()
Dim iResponse As Integer
Dim tTable As Table
'If any tables exist, loop through each table in collection.
For Each tTable In ActiveDocument.Tables
tTable.Select
iResponse = MsgBox("Table found. Find next?", 68)
If response = vbNo Then Exit For 'User chose to leave search.
Next
MsgBox prompt:="Search Complete.", buttons:=vbInformation
End Sub

This macro deletes any rows with a blank cell in the first column:
Sub DeleteRows()
Dim TargetText As String
Dim oRow As Row
If Selection.Information(wdWithInTable) = False Then Exit Sub
TargetText = InputBox$("Enter target text:", "Delete Rows")
For Each oRow In Selection.Tables(1).Rows
If oRow.Cells(1).Range.Text = TargetText & vbCr & Chr(7) Then oRow.Delete
Next oRow
End Sub

If anyone could modify the second macro to delete any rows with a blank cell in the third column and then nest it within the first macro to enable multiple tables to be amended, I'm sure that would solve my problem. I wish I was better at VBA to be able to do this for myself.

Thanks in advance if anyone can combine those for me with that slight change to the second macro.
4
Sub FindTables()
Dim iResponse As Integer
Dim tTable As Table
Dim TargetText As String
Dim oRow As Row

TargetText = InputBox("Enter target text:", "Delete Rows") 'Place this code between the lines with the For
Each statement to enter different target text for different tables

For Each tTable In ActiveDocument.Tables
For Each oRow In tTable.Rows
If oRow.Cells(2).Range.Text = TargetText & vbCr & Chr(7) Then oRow.Delete
Next oRow
Next tTable

End Sub
1