目录
一、代码
二、说明
一、代码
Function 是合并单元格(myCell As Cell) As StringDim results(1 To 3) As VariantDim tb As TableDim maxRow As LongDim maxCol As LongDim nextCell As CellDim i As LongDim 有合并行 As Boolean: 有合并行 = FalseDim 合并行数 As Long: 合并行数 = 0Dim 有合并列 As Boolean: 有合列行 = FalseDim 合并列数 As Long: 合并列数 = 0Set tb = myCell.Range.Tables(1)maxRow = tb.Rows.countmaxCol = tb.Columns.countOn Error Resume NextFor i = myCell.RowIndex + 1 To maxRowSet nextCell = tb.Cell(i, myCell.ColumnIndex)Do While Err.Number = 5941If i > maxRow ThenExit DoEnd IfErr.Cleari = i + 1Set nextCell = tb.Cell(i, myCell.ColumnIndex)Loop合并行数 = i - myCell.RowIndexIf 合并行数 > 1 Then有合并行 = TrueEnd IfExit ForNextFor i = myCell.ColumnIndex + 1 To maxColSet nextCell = tb.Cell(myCell.RowIndex, i)Do While Err.Number = 5941If i > maxCol ThenExit DoEnd IfErr.Cleari = i + 1Set nextCell = tb.Cell(myCell.RowIndex, i)Loop合并列数 = i - myCell.ColumnIndexIf 合并列数 > 1 Then有合并列 = TrueEnd IfExit ForNextOn Error GoTo 0results(1) = 有合并行 Or 有合并列results(2) = 合并行数results(3) = 合并列数是合并单元格 = VBA.Join(results, ";")End Function
二、说明
函数返回结果格式为:
True;2;1
以分号(;)为标志,分为3部分:
第一部分,表明是否为合并单元格(True是,False否);
第二部分,合并行的数值,比如2表示此单元格合并了2行;
第三部分,合并列的数值,比如1表示此单元格合并了1列(1列即没有合并列)。