我最近开始用键盘快捷键编写一个简单的宏合并/取消合并单元格。
宏目前正在使用以下代码:
If Selection.MergeCells = True Then
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = True
End With
Selection.UnMerge
ElseIf Selection.MergeCells = False Then
With Selection
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlBottom
.WrapText = False
.Orientation = 0
.AddIndent = False
.IndentLevel = 0
.ShrinkToFit = False
.ReadingOrder = xlContext
.MergeCells = False
End With
Selection.Merge
End If
这非常有效,但我最初有一个更简单的子功能,但是没有用。那是:
If Selection.MergeCells = False Then Selection.Merge
If Selection.MergeCells = True Then Selection.UnMerge
这个两行版本只能合并单元格,而不是合并它们。有谁知道为什么会这样?
感谢。
-Sean
答案 0 :(得分:2)
您需要 ELSE
Sub qwerty()
If Selection.MergeCells = False Then
Selection.Merge
Else
Selection.UnMerge
End If
End Sub