0
votes

So I have this VBA Code to delete the rows in Word tables if the 6th cells of those tables are empty. But the thing is not every table has 6 columns so this code deletes all my tables with 5 columns or less. Can you guys help me out to add a function that counts the number of columns if it's smaller than 6 then exit sub? Sorry for my bad English.

    Sub BCPISADeleteEmptyRows()
Dim tbl As Table, cel As Cell
Dim i As Long, j As Long, n As Long, fEmpty As Boolean
    Application.ScreenUpdating = False
    With ActiveDocument
        For Each tbl In .Tables
            n = tbl.Rows.Count
                        For i = n To 1 Step -1
                    fEmpty = True
                    For j = 6 To tbl.Rows(i).Cells.Count
                        Set cel = tbl.Rows(i).Cells(j)
                        If Len(Trim(cel.Range.Text)) > 2 Then
                         fEmpty = False
                        Exit For
                    End If
                Next j
                If fEmpty = True Then tbl.Rows(i).Delete
            Next i
        Next tbl
    End With
    Set cel = Nothing: Set tbl = Nothing
    Application.ScreenUpdating = True
End Sub
1

1 Answers

0
votes

For example:

Sub BCPISADeleteEmptyRows()
Application.ScreenUpdating = False
Dim Tbl As Table, r As Long
For Each Tbl In ActiveDocument.Tables
  With Tbl
    If .Columns.Count > 5 Then
      For r = .Rows.Count To 2 Step -1
        If Len(Trim(.Cell(r, 6).Range.Text)) = 2 Then .Rows(r).Delete
      Next
    End If
  End With
Next
Application.ScreenUpdating = True
End Sub