结合或合并单词 table 与条件

Combine or merge word table with condition

包含多个table的word文档。如果任何 table 的行等于值 x(例如 12),则删除第一行并剪切 table 并将其粘贴到前一个 table 的最后一行下方(意味着加入选定的 table 与前面的 table 放在最后)。下面的代码删除第一行,但将相同的 table 粘贴到所选 table 上方。我在下面的代码中使用了复制而不是剪切。

Private Sub MergeTables()
Dim tbl As Table, r As Row
Dim i As Integer

With ActiveDocument
For Each tbl In ActiveDocument.Tables
    If tbl.Rows.Count = 12 Then
    With tbl
    .Rows(1).Delete
    tbl.Range.Copy 'Need to paste below last row of previous table

    'tbl.Range.GoTo What:=wdGoToTable, Which:=wdGoToPrevious 'Pasting above the same table
    tbl.Range.PasteAndFormat wdFormatOriginalFormatting
    End With
    End If

Next tbl
End With
End Sub

提供第一个 table 的行数 <> 12:

Sub MergeTables()
Dim TblSrc As Table, TblTgt As Table, Rng As Range
For Each TblSrc In ActiveDocument.Tables
    With TblSrc
      If .Rows.Count = 12 Then
        .Rows(1).Delete
        Set Rng = TblTgt.Range
        Rng.Collapse wdCollapseEnd
        Rng.FormattedText = .Range.FormattedText
        .Delete
      Else
        Set TblTgt = TblSrc
      End If
    End With
Next
End Sub