Sub 合并单元格() Dim Rng As Range, PreviousRng As Range, TargetRng As Range If TypeName(Selection) <> "Range" Then Exit Sub If Selection.Columns.Count > 1 Then MsgBox "请选择一列区域": Exit Sub Set TargetRng = Intersect(ActiveSheet.UsedRange, Selection) Set PreviousRng = TargetRng(1) Application.DisplayAlerts = False For Each Rng In TargetRng.Offset(1, 0) If Rng <> Rng.Offset(-1, 0) Then Range(PreviousRng, Rng.Offset(-1, 0)).Merge Set PreviousRng = Rng End If Next Application.DisplayAlerts = TrueEnd Sub
Sub 取消合并单元格() Dim Rng As Range Application.FindFormat.Clear Application.FindFormat.MergeCells = True If TypeName(Selection) <> "Range" Then Exit Sub If Selection.MergeCells = False Then Exit Sub Do Set Rng = Selection.Find("", , , , , , , , True) With Rng.MergeArea .UnMerge .Value = Rng.Value End With Set Rng = Selection.Find("", Rng, , , , , , , True) If Rng Is Nothing Then Exit Do LoopEnd Sub