在VBA中合并和居中单元格
我最近着手写一个简单的macros,用键盘快捷键合并/取消合并单元格。
macros目前正在使用以下代码:
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
你需要一个ELSE
Sub qwerty() If Selection.MergeCells = False Then Selection.Merge Else Selection.UnMerge End If End Sub