user20159
user20159

Reputation: 175

Merging content of cells with VBA

I'm trying to merge the content of some cells into one, but each piece of information in one line within the cell. Does anyone know how to do it with VBA? I attach a picture to help explain it.

enter image description here

Upvotes: 0

Views: 198

Answers (1)

Netloh
Netloh

Reputation: 4378

This piece of code should do the trick:

Sub MergeContents()
    Dim i As Integer

    For i = 2 To ActiveSheet.Cells(Rows.Count, 1).End(xlUp).Row Step 1
        ActiveSheet.Cells(i, 9).Value = _
            ActiveSheet.Cells(1, 1).Value & ": " & ActiveSheet.Cells(i, 1).Value & Chr(10) & _
            ActiveSheet.Cells(1, 2).Value & ": " & ActiveSheet.Cells(i, 2).Value & Chr(10) & _
            ActiveSheet.Cells(1, 3).Value & ": " & ActiveSheet.Cells(i, 3).Value & Chr(10) & _
            ActiveSheet.Cells(1, 4).Value & ": " & ActiveSheet.Cells(i, 4).Value
    Next i

End Sub

Upvotes: 1

Related Questions