Delete specific table rows in Word
I have found the following code which works well with a document with one Word table. I need the code to go through the entire document which has a large number of tables. All the tables are two columns (multiple rows) and I need to delete the whole row if it contains a certain word in cell 1.
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
I would appreciate any assistance you can offer.
Thanks:)
Re: Delete specific table rows in Word
vb Code:
dim t as table
for each t in activedocument.tables
For Each oRow In t.rows
If oRow.Cells(1).Range.Text = TargetText & vbCr & Chr(7) Then oRow.Delete
Next oRow
next t
Re: Delete specific table rows in Word
Moved To Office Development
Re: Delete specific table rows in Word
Many thanks for providing a quick answer to my question, however, I am still having trouble. Although the attached document appears to have the text in tables, the VB code will not run. I only want to delete the row if it only contains the text "N/A" (which I have highlighted in yellow).
Thanks in advance.
Re: Delete specific table rows in Word
here is some working code
there were 2 issues, table within tables and i could not make the test string = to the cell content, even though i could see that they were identical and checked the ascii values of every character in both strings
vb Code:
Sub DeleteRows()
Dim d As Document, t As Table, t1 As Table
Dim TargetText As String
Dim oRow As Row
Set d = ActiveDocument
If Selection.Information(wdWithInTable) = False Then Exit Sub
TargetText = "N/A " 'InputBox$("Enter target text:", "Delete Rows")
For Each t In d.Tables
For Each t1 In t.Tables
For Each oRow In t1.Rows
If InStr(oRow.Cells(1).Range.Text, TargetText) = 1 Then oRow.Delete
Next
Next
Next
End Sub
give it a good test first
Re: Delete specific table rows in Word
Once again, a big thank you for the code which you provided, it works like a charm! I really do appreciate the time you have taken to resolve my problem.