【问题标题】:Prevent Vertically Merged Cells from Breaking Across Page - Automatically防止垂直合并的单元格跨页 - 自动
【发布时间】:2025-12-26 06:40:16
【问题描述】:

我必须创建将大型数据表从 Excel 复制到其中的文档。这些表可以有数百行长,一般约为 20 列宽。许多列已垂直合并,以增强可读性和分组数据集。

我已经能够编写一个宏来完全格式化整个表格,但我无法弄清楚如何自动防止垂直合并的单元格在多个页面上中断/拆分。要手动执行此操作,请选择合并中除最后一行之外的所有行,然后在段落设置中打开“保留下一个”。我认为这很容易做到,但是如果表格中有任何垂直合并的单元格,您将无法访问 VBA 中的各个行。

有没有人知道如何自动遍历行并为已合并在一起的行组设置“保留下一个”属性?

以下是 Word 通常如何跨表格处理垂直合并单元格的示例:

这是我希望它看起来的样子,手动完成所有工作:

【问题讨论】:

    标签: vba ms-word


    【解决方案1】:

    是的,在 Word(以及 Excel)中处理合并的单元格非常烦人。

    不过,这可以通过访问表格中的单个单元格来完成。我在下面编写了以下子例程,它应该适合你。我假设您至少有一列没有垂直合并的单元格,并且您只有一列控制合并块的长度。虽然添加更多控制列应该很容易。

    Sub MergedWithNext() 'FTable As Table)
    
    Dim Tester As String
    Dim FTable As Table
    Dim i As Integer
    Dim imax As Integer
    Dim RowStart As Integer
    Dim RowEnd As Integer
    Dim CNMerged As Integer
    Dim CNNotMerged As Integer
    Dim CNMax As Integer
    
    CNMerged = 2 'A column number that is vertically merged that you don't want to split pages
    CNNotMerged = 1 'A column number that has no vertical mergers
    
    Set FTable = Selection.Tables(1)
    
    With FTable
    imax = .Rows.Count
    CNMax = .Columns.Count
    
    'Start with no rows kept with next
    ActiveDocument.Range(Start:=.Cell(1, 1).Range.Start, _
        End:=.Cell(imax, CNMax).Range.End).ParagraphFormat.KeepWithNext = False
    
    On Error Resume Next
    For i = 2 To imax 'Assume table has header
    
        Tester = .Cell(i, CNMerged).Range.Text 'Test to see if cell exists
        If Err.Number = 0 Then 'Only the first row in the merged cell will exist, others will not
    
            'If you are back in this If statement, then you have left the previous block of rows
            'even if that was a block of one. The next If statement checks to see if the previous
            'row block had more than one row. If so it applies the "KeepWithNext" property
    
            If (RowEnd = (i - 1)) Then
    
                '.Cell(RowStart, 1).Range.ParagraphFormat.KeepWithNext = True
                ActiveDocument.Range(Start:=.Cell(RowStart, CNNotMerged).Range.Start, _
                    End:=.Cell(RowEnd - 1, CNNotMerged).Range.End).ParagraphFormat.KeepWithNext = True
    
                    'Use RowEnd - 1 because you don't care if the whole merged block stays with the next
                    'row that is not part of the merger block
    
            End If
    
            RowStart = i 'Beginning of a possible merger block
            RowEnd = 0 'Reset to 0, not really needed, used for clarity
    
        Else
    
            RowEnd = i 'This variable will be used to determine the last merged row
            Err.Clear
    
        End If
    
        If i = imax Then 'Last Row
    
            If (RowStart <> imax) Then
    
                ActiveDocument.Range(Start:=.Cell(RowStart, CNNotMerged).Range.Start, _
                    End:=.Cell(imax - 1, CNNotMerged).Range.End).ParagraphFormat.KeepWithNext = True
    
                    'Use imax - 1 because you don't care if the whole merged block stays with the next
                    'row that is not part of the merger block
    
            End If
    
        End If
    
    Next i
    On Error GoTo 0
    End With
    End Sub
    

    此代码将遍历表格中的每一行,不包括标题,寻找垂直合并的单元格。一旦找到一个块,它将为块中的每一行分配“保留下一个”属性,最后一行除外。

    【讨论】:

    • 我意识到如果原始代码与最后一行合并,它会错过倒数第二行。我添加了一个新代码块,以确保最后两行在合并时保持在一起。