Home > Excel > Timesaving macro concatenates cell values

Timesaving macro concatenates cell values

November 8th, 2010

While working with spreadsheet data, it’s common to face situations where you have to manually reorder or transform data. Macros are of good help then. I’ll share an example of a quite common case where simple macro can help save a lot of time.

Case: the spreadsheet contains manually entered and badly structured data. You need to paste together the data from various cells, but you can’t do it with formulas because every situation is slightly different (contains different number of rows e.g.). Basically, you need a tool that takes the contents of currently selected cells, concatenates the values and pastes the outcome somewhere.

Problem and the desired outcome is depicted on the following screenshot.

As a solution, you can use a simple macro that takes the contents of the selection, concatenates it using newlines between values and pastes the outcome to the first selected cell. The code that does this is on the following screenshot. It uses Selection.Cells property that refers to the currently selected range of cells.

Märt Parker Excel ,

  1. Märt Parker
    November 16th, 2010 at 14:41 | #1

    Here is the code for copy/pasting.

    Sub Macro1()

    Dim s As String
    Dim i As Integer
    i = 0

    For Each Cell In Selection.Cells
    If (i > 0) Then
    s = s & Chr(10) & Cell.Value
    Else
    s = Cell.Value
    End If
    Cell.Value = “”
    i = i + 1

    Next

    Selection.Cells(1, 1).Value = s

    End Sub

  2. cris
    July 27th, 2011 at 04:07 | #2

    doesn’t work
    move all to one cell , without coma

  3. cris
    July 27th, 2011 at 04:14 | #3

    should be :
    s = s & Chr(44) & Chr(10) & Cell.Value

  1. No trackbacks yet.