How to Merge MS Excel Cells and Justify the Text with Macro

Merging MS Excel cells and centering the contents is so easy: you just select the cells and click the Merge & Center button on the Home tab of the ribbon.

But what if you don;t want the content to center? What if you’d like to left- or right-justify the text within the merged cells?

Then you need a simple macro.

1) Press Alt + F11 to display the VBA Editor.

2) Select the Worksheet you’re working on from the VBA Project pane.

3) Right click and select New > Module to create a new module.

4) Click the new module to enable its code pane on the right:

MS Excel Merge and Left or Right Justify 1
5) In the code pane, enter this VBA code:

Sub MergeRight()
    With Selection
        .HorizontalAlignment = xlRight
        .Merge
    End With
End Sub

6) Save your workbook and switch back to your worksheet (Alt + F11).

7) Select the cell range you like.

MS Excel Merge and Left or Right Justify 2
8) On the ribbon, select Developer. Click Macros to display the list of macros recorded. Your MergeRight macro should be there:

MS Excel Merge and Left or Right Justify 3

9) Select the macro and click Run to right-merge all your content:

MS Excel Merge and Left or Right Justify 4
Similarly, to merge and left-justify your content, you can change the “xlRight” in the VBA formula to “xlLeft“.
But even simpler, just select the Merge Across option from the Merge & Center drop-down list:
MS Excel Merge and Left or Right Justify 5