Sub 合并单元格()
'功能:根据第2列(班别)是否相同,进行对第1、2、7列合并单元格

Dim m, n, t, row, rowcount As Long
rowcount = ActiveSheet.UsedRange.Rows.Count + 1
Application.ScreenUpdating = False '关闭屏幕更新
Application.DisplayAlerts = False
t = 1 '序号
m = 2
'n为行号
For n = 3 To rowcount
'根据第2列(班别)是否相同,进行合并单元格
    If Cells(n, 2).Value <> Cells(n - 1, 2).Value And m < n Then
    '合并第2列(班别)
        With Range(Cells(m, 2), Cells(n - 1, 2))
            .Merge
        End With
        '合并第7列(周总课时),并计算周总课时
        With Range(Cells(m, 7), Cells(n - 1, 7))
            .Merge
            .Value = Application.WorksheetFunction.Sum(Range(Cells(m, 6), Cells(n - 1, 6)))
        End With
        '合并第1列(序号列),并填写序号
        With Range(Cells(m, 1), Cells(n - 1, 1))
            .Merge
            .Value = t
            t = t + 1
        End With
        m = n
    End If
    If Cells(n, 2).Value = "" Then
        m = n + 1
    End If
Next n
Application.ScreenUpdating = True '恢复屏幕更新

End Sub

Sub 取消合并单元格()
'功能:取消当前工作表所有合并单元格
Dim i As Range
For Each i In ActiveSheet.UsedRange
If i.Address <> i.MergeArea.Address And i.Address = i.MergeArea.Item(1).Address Then
i.MergeArea.Select
i.MergeArea.UnMerge
' MsgBox (i.MergeArea.Address)
Selection.FillDown
End If
Next i
End Sub

标签: none

评论已关闭