Results 1 to 6 of 6

Thread: Delete specific table rows in Word

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    3

    Resolved 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
    Last edited by Gizzmo1; Apr 12th, 2009 at 07:39 AM. Reason: Resolved

  2. #2
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: Delete specific table rows in Word

    vb Code:
    1. dim t as table
    2. for each t in activedocument.tables
    3.   For Each oRow In t.rows
    4.     If oRow.Cells(1).Range.Text = TargetText & vbCr & Chr(7) Then oRow.Delete
    5.   Next oRow
    6. next t
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  3. #3
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: Delete specific table rows in Word

    Moved To Office Development

  4. #4

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    3

    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.
    Last edited by Gizzmo1; Apr 12th, 2009 at 07:43 AM. Reason: Still having trouble

  5. #5
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    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:
    1. Sub DeleteRows()
    2. Dim d As Document, t As Table, t1 As Table
    3. Dim TargetText As String
    4. Dim oRow As Row
    5. Set d = ActiveDocument
    6. If Selection.Information(wdWithInTable) = False Then Exit Sub
    7.  
    8. TargetText = "N/A " 'InputBox$("Enter target text:", "Delete Rows")
    9. For Each t In d.Tables
    10.     For Each t1 In t.Tables
    11.         For Each oRow In t1.Rows
    12.        
    13.             If InStr(oRow.Cells(1).Range.Text, TargetText) = 1 Then oRow.Delete
    14.         Next
    15.     Next
    16. Next
    17. End Sub
    give it a good test first
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  6. #6

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    3

    Smile 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.

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width