Search code examples
excelms-wordexcel-2010excel-2007vba

Delete Row of a Word Table if Specific Value Exists using Excel VBA


So I have a table on a word document and was wondering how I could rewrite this in order to delete the entire row of the table (the text existing in all the cells in the row as well as the existence of the row itself) if the value in the first column is an empty cell:

Set wdTable = wdDoc.Tables(1)
With wdTable.Cells(i,1)
    For i = 2 To wdTable.Rows.Count
        If .Value = "" Then
           .EntireRow.Delete
        End If
    Next i
End With

Solution

  • You mean like this?

    Set wdTable = wdDoc.Tables(1)
    
    For i = wdTable.Rows.Count To 2 Step -1
        If wdTable.Cell(i, 1).Range.Text = Chr(13) & Chr(7) Then wdTable.Rows(i).Delete
    Next i
    

    Note:

    1. You have to loop through the rows in reverse
    2. Even when the cell looks empty it is not, it has Chr(13) & Chr(7) and hence you cannot use =""
    3. You cannot use .Cells. It is .Cell
    4. There is no property as .Value. It is .Range.Text

    Screenshot

    enter image description here