pratnimk,
Welcome to the MrExcel forum.
Here is another macro solution for you to consider, that uses two arrays in memory, and, will adjust to the number of raw data rows, and, columns.
Sample raw data in the active worksheet:
Excel 2007
<colgroup><col style="width: 25pxpx"><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="bgcolor: #FFFFFF, align: right"][/TD]
[TD="bgcolor: #FFFFFF"]A[/TD]
[TD="bgcolor: #FFFFFF"]D[/TD]
[TD="bgcolor: #FFFFFF, align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]2[/TD]
[TD="bgcolor: #FFFFFF, align: right"][/TD]
[TD="bgcolor: #FFFFFF"]B[/TD]
[TD="bgcolor: #FFFFFF, align: right"][/TD]
[TD="bgcolor: #FFFFFF, align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]3[/TD]
[TD="bgcolor: #FFFFFF, align: right"][/TD]
[TD="bgcolor: #FFFFFF"]C[/TD]
[TD="bgcolor: #FFFFFF"]E[/TD]
[TD="bgcolor: #FFFFFF"]F[/TD]
[TD="align: right"][/TD]
[TD="align: center"]4[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
</tbody>
Sheet1
And, after the macro:
Excel 2007
<colgroup><col style="width: 25pxpx"><col><col><col><col><col></colgroup><thead>
</thead><tbody>
[TD="align: center"]1[/TD]
[TD="bgcolor: #FFFFFF"]A, D[/TD]
[TD="bgcolor: #FFFFFF"]A[/TD]
[TD="bgcolor: #FFFFFF"]D[/TD]
[TD="bgcolor: #FFFFFF, align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]2[/TD]
[TD="bgcolor: #FFFFFF"]B[/TD]
[TD="bgcolor: #FFFFFF"]B[/TD]
[TD="bgcolor: #FFFFFF, align: right"][/TD]
[TD="bgcolor: #FFFFFF, align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: center"]3[/TD]
[TD="bgcolor: #FFFFFF"]C, E, F[/TD]
[TD="bgcolor: #FFFFFF"]C[/TD]
[TD="bgcolor: #FFFFFF"]E[/TD]
[TD="bgcolor: #FFFFFF"]F[/TD]
[TD="align: right"][/TD]
[TD="align: center"]4[/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
[TD="align: right"][/TD]
</tbody>
Sheet1
Please TEST this FIRST in a COPY of your workbook (always make a backup copy before trying new code, you never know what you might lose).
1. Copy the below code
2. Open your NEW workbook
3. Press the keys
ALT +
F11 to open the Visual Basic Editor
4. Press the keys
ALT +
I to activate the Insert menu
5. Press
M to insert a Standard Module
6. Where the cursor is flashing, paste the code
7. Press the keys
ALT +
Q to exit the Editor, and return to Excel
8. To run the macro from Excel press
ALT +
F8 to display the Run Macro Dialog. Double Click the macro's name to Run it.
Code:
Sub MergeData()
' hiker95, 02/08/2017, ME990036
Dim a As Variant, i As Long, c As Long
Dim o As Variant, lr As Long, lc As Long
Application.ScreenUpdating = False
With ActiveSheet
lr = .Cells.Find("*", , xlValues, xlWhole, xlByRows, xlPrevious, False).Row
lc = .Cells.Find("*", , xlValues, xlWhole, xlByColumns, xlPrevious, False).Column
a = .Range(.Cells(1, 2), .Cells(lr, lc))
ReDim o(1 To UBound(a, 1), 1 To 1)
For i = LBound(a, 1) To UBound(a, 1)
o(i, 1) = a(i, 1)
For c = LBound(a, 2) + 1 To UBound(a, 2)
If Not a(i, c) = vbEmpty Then
o(i, 1) = o(i, 1) & ", " & a(i, c)
End If
Next c
Next i
.Cells(1, 1).Resize(UBound(o, 1), UBound(o, 2)) = o
.Columns(1).AutoFit
End With
Application.ScreenUpdating = True
End Sub
Then run the
MergeData macro.