title: Merging and Centering Cells in VBA
body: <p>I recently set out to write a simple macro the merge/unmerge cells with a keyboard shortcut.</p>
  
  <p>The macro is working currently with the following code:</p>
  
  <pre><code>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
  </code></pre>
  
  <p>This works perfectly well, but I originally had a much simpler sub that didn't work.  It was:</p>
  
  <pre><code>If Selection.MergeCells = False Then Selection.Merge
  If Selection.MergeCells = True Then Selection.UnMerge
  </code></pre>
  
  <p>This two-line version only worked to merge cells, not to unmerge them.  Does anyone know why this was happening?</p>
  
  <p>Thanks.</p>
  
  <p>-Sean</p>