Word VBA:判断一个单元格是否为合并单元格

目录

一、代码

二、说明


一、代码

Function 是合并单元格(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 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
    
    On Error Resume Next
    For i = myCell.RowIndex + 1 To maxRow
        Set nextCell = tb.Cell(i, myCell.ColumnIndex)
        Do While Err.Number = 5941
            If i > maxRow Then
                Exit Do
            End If
            Err.Clear
            i = i + 1
            Set nextCell = tb.Cell(i, myCell.ColumnIndex)
        Loop
        
        合并行数 = i - myCell.RowIndex
        If 合并行数 > 1 Then
            有合并行 = True
        End If
        Exit For
    Next
    
    For i = myCell.ColumnIndex + 1 To maxCol
        Set nextCell = tb.Cell(myCell.RowIndex, i)
        Do While Err.Number = 5941
            If i > maxCol Then
                Exit Do
            End If
            Err.Clear
            i = i + 1
            Set nextCell = tb.Cell(myCell.RowIndex, i)
        Loop
        
        合并列数 = i - myCell.ColumnIndex
        If 合并列数 > 1 Then
            有合并列 = True
        End If
        Exit For
    Next
    On Error GoTo 0
    
    results(1) = 有合并行 Or 有合并列
    results(2) = 合并行数
    results(3) = 合并列数
    
    是合并单元格 = VBA.Join(results, ";")
End Function

二、说明

函数返回结果格式为:

True;2;1

以分号(;)为标志,分为3部分:

第一部分,表明是否为合并单元格(True是,False否);

第二部分,合并行的数值,比如2表示此单元格合并了2行;

第三部分,合并列的数值,比如1表示此单元格合并了1列(1列即没有合并列)。

  • 1
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

VBA-守候

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值