|
楼主 |
发表于 2023-5-4 21:09
|
显示全部楼层
前面的代码情况非常复杂时确实有问题
可以试试下面补充方法
- Function 是合并单元格_Selection方法(myCell As Cell) As String
- Dim results(1 To 3) As Variant
- Dim tb As Table
- Dim maxRow As Long
- Dim maxCol As Long
- Dim myRow As Long
- Dim myCol As Long
- Dim nextCell As Cell
- Dim i As Long
- Dim 有合并行 As Boolean: 有合并行 = False
- Dim 合并行数 As Long: 合并行数 = 0
- Dim 有合并列 As Boolean: 有合列行 = False
- Dim 合并列数 As Long: 合并列数 = 0
-
- Set tb = myCell.Range.Tables(1)
-
- maxRow = tb.Rows.Count
- maxCol = tb.Columns.Count
- myRow = myCell.RowIndex
- myCol = myCell.ColumnIndex
-
- myCell.Select
- Selection.Collapse
- If myCell.Range.Start >= tb.Range.Cells(tb.Range.Cells.Count).Range.Start Then
- 合并列数 = maxCol - myCol + 1
- Else
- Selection.MoveRight wdCell, 1
- If Selection.Cells(1).ColumnIndex > myCol Then
- 合并列数 = Selection.Cells(1).ColumnIndex - myCol
- Else
- 合并列数 = maxCol - myCol + 1
- End If
- End If
- If 合并列数 > 1 Then
- 有合并列 = True
- End If
-
-
- myCell.Select
- Selection.Collapse 0
-
- Selection.MoveDown wdLine
- If Selection.Start >= tb.Range.End Then
- 合并行数 = maxRow - myRow + 1
- Else
- 合并行数 = Selection.Cells(1).RowIndex - myRow
- End If
- If 合并行数 > 1 Then
- 有合并行 = True
- End If
-
- results(1) = 有合并行 Or 有合并列
- results(2) = 合并行数
- results(3) = 合并列数
- 是合并单元格_Selection方法 = VBA.Join(results, ";")
- End Function
- Sub 测试_标题合并单元格情况()
- Dim cel As Cell
- Dim s As String
- For Each cel In ActiveDocument.Tables(2).Range.Cells
- s = 是合并单元格_Selection方法(cel)
- cel.Range.Text = s
- Debug.Print s
- Next
- End Sub
复制代码 |
|