Concatenate text and insert carriage returns using macro

I would like to be able to select text from several rows or columns, then run a macro that will combine the cells into one cell with a carriage return CHAR(10) inserted between the text from each cell.  I've seen that this can be done using a formula, but i would prefer to just select the text and hit a button.  Then the macro would prompt me to paste is somewhere or alternatively, it could just enter all the text with the carriage returns into the first cell i selected.
LVL 1
antimelAsked:
Who is Participating?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

zorvek (Kevin Jones)ConsultantCommented:
Place this code in the worksheet code module. Select the cells and run macro "Step1". Click in any cell to paste the result.

Private Value As String

Public Sub Step1()

    Dim Cell As Range

    Value = vbNullString
    For Each Cell In Selection
        Value = Value & IIf(Len(Value) > 0, vbLf, vbNullString) & Cell
    Next Cell

End Sub

Private Sub Worksheet_SelectionChange(ByVal Target As Range)

    If Target.Cells.Count = 1 Then
        If Len(Value) > 0 Then
            ActiveCell.Value = Value
            Value = vbNullString
        End If
    End If

End Sub

Kevin
0
jppintoCommented:
Just select a range of cells and run this code. It will display the result on a mesage box. You can put it on a place on your sheet if you want.

jppinto
Sub JoinCells()
Dim result As String
Dim i As Long

For i = 1 To Selection.Cells.Count
    If Selection.Cells(i, 1).Value <> "" Then
       result = result & Selection.Cells(i, 1).Value & Chr(10)
    End If
Next i

MsgBox (result)

End Sub

Open in new window

0
antimelAuthor Commented:
Zorvek, your solution didn't appear to do anything when i ran it.

ippinto, how do you paste the data onto the sheet instead of showing a message box.
0
Fundamentals of JavaScript

Learn the fundamentals of the popular programming language JavaScript so that you can explore the realm of web development.

zorvek (Kevin Jones)ConsultantCommented:
You have to put the code in the worksheet code module, not a general code module.

Kevin
0
antimelAuthor Commented:
Zorvek,

I have built a add-in where i keep all my macros, i'd like to add it to that so i can run this on any excel document.  Is there a way to do that.  I have about a hundred different excel files where i need to use this macro.
0
zorvek (Kevin Jones)ConsultantCommented:
A different approach. Select the cells, run the macro below, select the target cell (double click to enter text) and press CTRL+V.

Public Sub CopyCellsToClipboard()

    Dim Clipboard As Object
    Dim Cell As Range
    Dim Value As String

    Set Clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
   
    Value = vbNullString
    For Each Cell In Selection
        Value = Value & IIf(Len(Value) > 0, vbLf, vbNullString) & Cell
    Next Cell
    Clipboard.SetText Value
    Clipboard.PutInClipboard
   
End Sub

Kevin
0
jppintoCommented:
Something like this...
Sub JoinCells()
Dim result As String
Dim i As Long

For i = 1 To Selection.Cells.Count
    If Selection.Cells(i, 1).Value <> "" Then
        If i <> Selection.Cells.Count Then
            result = result & Selection.Cells(i, 1).Value & Chr(10)
        Else
            result = result & Selection.Cells(i, 1).Value
        End If
    End If
Next i

Cells(1, 10).Value = result

End Sub

Open in new window

0
antimelAuthor Commented:
ippinto, It seems to just paste into J1, can i shoose where to paste it.
0
zorvek (Kevin Jones)ConsultantCommented:
A two macro approach. Select the source cells, run the first macro, select the target cell, run the second macro.

Public Sub Step1()

    Dim Clipboard As Object
    Dim Cell As Range
    Dim Value As String

    Set Clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
   
    Value = vbNullString
    For Each Cell In Selection
        If Len(Cell.Value) > 0 Then
            Value = Value & IIf(Len(Value) > 0, vbLf, vbNullString) & Cell
        End If
    Next Cell
    Clipboard.SetText Value
    Clipboard.PutInClipboard
   
End Sub

Public Sub Step2()

    Dim Clipboard As Object
    Dim ClipboardText As String

    Set Clipboard = GetObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")

    Clipboard.GetFromClipboard
    ClipboardText = vbNullString
    On Error Resume Next
    ClipboardText = Clipboard.GetText
    On Error GoTo 0
    If Len(ClipboardText) > 0 Then
        ActiveCell.Value = ClipboardText
    End If

End Sub

Kevin
0

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
Grasty86Commented:
If you are using Excel, which you hinted to in your zone you chose, you can use the following

=CONCATENATE(A1,CHAR(10),B1)

This would combine the text in A1 and B1 with a carriage return in between

Note: This wont work unless you have wrap text on for the cell you are using to combine them.
0
zorvek (Kevin Jones)ConsultantCommented:
"I've seen that this can be done using a formula, but i would prefer to just select the text and hit a button."
0
Grasty86Commented:
My mistake, didnt read the whole thing.
0
antimelAuthor Commented:
An elegant solution, it worked perfectly, and I added it to my macro add-in.  Thank you for helping me out on this.

Anthony
0
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Microsoft Excel

From novice to tech pro — start learning today.