Word Macro Remove Rows from Table If Cell Empty

血红的双手。 提交于 2021-02-10 20:38:37

问题


I am trying to run a Macro that will check a selected table for empty cells in column 2, and if there are empty cells, delete that row.

Sub DeleteEmptyRows()
Set Tbl = Selected.Tables(1)
    With Tbl
        noOfCol = Tbl.Range.Rows(1).Cells.Count
        With .Range
            For i = .Cells.Count To 1 Step -1
                On Error Resume Next
                If Len(.Cells(i).Range) = 2 Then
                    .Rows(.Cells(i).RowIndex).Delete
                    j = i Mod noOfCol
                    If j = 0 Then j = noOfCol
                End If
            Next i
    End With
    End With

End Sub

And it's really close to what I want, but I'm just not sure how to specify empty cells in column 2. I also tried changing the noOfCol line to:

Selection.SetRange Selection.Tables(1).Rows(2).Cells(2).Range.Start, _
Selection.Tables(1).Rows.Last.Cells(2).Range.End

But that still deletes rows where any column is empty. I need it to delete only rows where column 2 is empty. Thanks


回答1:


Working with all the cells in column is a problem because it's not possible to set a Range to a column. A Range must be a continguous set of characters in the document. While a column looks contiguous, behind the scenes its content is actually not. The characters of a table run from top-left to the right, and top-to-bottom (the rows).

The closest code can get is to select the column then work in the Selection object. Or loop the rows.

The following code sample demonstrates how to "loop the rows" - similar to what the code in the question uses. The key here is to use the Table.Cells for the loop, with the For counter designating the row index and the column number (2) designating the column index.

Sub ProcessColTwo()
    Dim tbl As Word.Table
    Dim nrRows As Long, ColToCheck As Long, i As Long
    Dim cellRange As Word.Range

    Set tbl = ActiveDocument.Tables(1)
    nrRows = tbl.Rows.Count
    ColToCheck = 2

    For i = nrRows To 1 Step -1
        Set cellRange = tbl.Cell(i, ColToCheck).Range
        If Len(cellRange.text) = 2 Then
            cellRange.Rows(1).Delete
        End If
    Next i
End Sub

And here's code that demonstrates using Selection

Sub ProcessColTwo()
    Dim tbl As Word.Table
    Dim ColToCheck As Long
    Dim cel As Word.Cell

    Set tbl = ActiveDocument.Tables(1)
    ColToCheck = 2
    tbl.Columns(ColToCheck).Select
    For Each cel In Selection.Cells
        If Len(cel.Range.text) = 2 Then
            cel.Range.Rows(1).Delete
        End If
    Next
End Sub



回答2:


The code below is for for "each and every" pages after a mail merge [i have used a nested loop ( one for [TABLE] and the other for empty [CELLS])

All codes [respect to: Cindy Meister]

  Sub ProcessColTwo()
     Dim pg As Page
     Dim tbl As Word.Table
     Dim ColToCheck As Long
     Dim cel As Word.Cell

       For Each tbl In ActiveDocument.Tables
       ColToCheck = 2
       tbl.Columns(ColToCheck).Select

       For Each cel In Selection.Cells
           If Len(cel.Range.Text) = 2 Then
              cel.Range.Rows(1).Delete
           End If
       Next
       Next
  End Sub


来源:https://stackoverflow.com/questions/53323710/word-macro-remove-rows-from-table-if-cell-empty

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!