TOP

Merge cells without losing text

Description

Suppose we have many cells that we need to merge together with text. The Excel standard set has a similar function: "Merge and center", but when performing the merge, it leaves only the text of the outermost cell. So we need to use our own macro for such purposes.


VBA code for the macro

To do this, open the editor Visual Basic (Alt+F11), insert VBA module (Insert - Module) and copy the text of this function there:

Sub MergeCell()
    Const sDELIM    As String = " "
    Dim   rCell     As Range
    Dim   sMergeStr As String
    
    If TypeName(Selection) <> "Range" Then Exit Sub
    
    With Selection
        For Each rCell In .Cells
            sMergeStr = sMergeStr & sDELIM & rCell.Text
        Next rCell
        
        Application.DisplayAlerts = False
        .Merge Across:=False
        Application.DisplayAlerts = True
        
        .Item(1).Value = Mid(sMergeStr, 1 + Len(sDELIM))
    End With
End Sub

Save and return to Excel.

Now select the required cells and run our macro (Alt+F8) (macro name: "MergeCell").