Thursday, January 31, 2019

VBA Code to Merge Two Cells without Losing Data in Ms Excel

VBA Code to Merge Two Cells without Losing Data in Ms Excel 


1. Open active excel sheet 

2. Press Alt + F11 and Visual Basic Application window will open.

3. Go to Insert Menu and select new Module.

4. In new Module insert the below VBA Code and save it.

5. Before do the above process, excel sheet must save as Macro-Enabled.

6. To do Macro Enabled, open Excel Sheet and Click on the Office Button which is at left top corner on excel sheet.

7.  Select Save as and again select Macro Enabled Worksheet and then save it.

VBA Code to Merge Two Cells without Losing Data in Ms Excel 


Sub JoinAndMerge()
Dim outputText As String
On Error Resume Next
For Each cell In Selection
outputText = outputText & cell.Value & delim
Next cell
With Selection
.Clear
.Cells(1).Value = outputText
.Merge
.HorizontalAlignment = xlGeneral
.VerticalAlignment = xlCenter
.WrapText = True
End With
End Sub

No comments:

Post a Comment