Macro to combine text from multiple cells?

dm59

Board Regular
Joined
Jul 13, 2006
Messages
73
Hello all
I have the following task that I'm hoping code will automate for me.

1. I select cells over several rows in a column, each of which has text in it.
2. I need the top cell to have the text from each of the other cells added to it, with a semi-colon after each of those entries.

So, now I have just one cell, the top one, with all of those entries in it.

Thanks in advance.
 

Excel Facts

Convert text numbers to real numbers
Select a column containing text numbers. Press Alt+D E F to quickly convert text to numbers. Faster than "Convert to Number"
Try this - select the cells then run the macro.

Code:
Sub concat()
Dim c As Range, txt As String
For Each c In Selection
    txt = txt & c.Value & "; "
Next c
Selection.ClearContents
txt = Left(txt, Len(txt) - 2)
Selection(1).Value = txt
End Sub
 
Upvote 0
In step 1 are the cells next to each other (eg A1,A2,A3) or are they randomly selected (eg A1, A3,A10)
 
Upvote 0
This does what you asked for. If you want the other source cells to be cleared, uncomment the red line.
Rich (BB code):
Sub ConcatenateCellsToActiveCell_Semicolon()
    Dim rngCell As Range
    Dim rngActive As Range
    Dim strTemp As String
 
    Set rngActive = Selection
    strTemp = ""
 
    For Each rngCell In rngActive
        strTemp = strTemp & rngCell.Value
        strTemp = strTemp & "; "
        'rngCell.Value = ""  'Uncomment to clear selected, non-active cells
    Next
    ActiveCell.Value = Left(strTemp, Len(strTemp) - 2)
 
    Set rngActive = Nothing
End Sub

Note: If you select a contiguous block of cells (row or column), the first cell you select is the active one. If you select separate single cells (by holding down Ctrl), the last cell you select is the active one. If you select separate regions, the first cell in the last region you select will be active.
 
Upvote 0
OMG. Do you guys know how cool you really are? I just used VoG II's code and it worked great. Thanks a bunch.

JimM
The cells are more like C1,C2,C3, etc.

I have a problem in the resulting cell though. It displays as "####....." in the cell but it shows the proper text in the formula bar. I tried changing the cell's format but I can't make it work.
 
Upvote 0
Never mind about the format issue. I figured it out.

Thanks again, everyone.

Darrell
 
Upvote 0
Try this - select the cells then run the macro.

Code:
Sub concat()
Dim c As Range, txt As String
For Each c In Selection
    txt = txt & c.Value & "; "
Next c
Selection.ClearContents
txt = Left(txt, Len(txt) - 2)
Selection(1).Value = txt
End Sub

Can this macro be modified such that the user selects the cell to which the data from multiple cells will ultimately be combined (i.e., the cell is not necessarily above the multiple cells)? In other words, if I want to create a list seperated by semicolons on cell c5 of sheet1 using entries from multiple cells from sheet 2, can the macro make that work? Does that make sense? Can it be done without clearing the original entries?

Ted
 
Last edited:
Upvote 0
This copies the selected cells, separated by semicolons to the clipboard. Paste where you desire.
Code:
Sub ConcatenateSelection_SemiColon_ToClipboard()

    Dim rngCell As Range
    Dim strTemp As String
    Dim strSeparator As String
    
    'Early Binding
    'Dim objMyData As New DataObject
    'Use Tools | References to add reference
    '"Microsoft Forms 2.0" (FM20.DLL) (or later version)
    
    'Late Binding for New DataObject
    Dim objMyData As Object
    Set objMyData = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    'Set objMyData = CreateObject("MSFORMS.DataObject") 'Logical, but doesn't work

    strTemp = ""
    strSeparator = ";"
    
    For Each rngCell In Selection
        strTemp = strTemp & rngCell.Value
        strTemp = strTemp & strSeparator
        'rngCell.Value = ""  'Uncomment this to clear other source cells
    Next
    objMyData.SetText Left(strTemp, Len(strTemp) - Len(strSeparator))
    objMyData.PutInClipboard

End Sub
 
Upvote 0
This copies the selected cells, separated by semicolons to the clipboard. Paste where you desire.
Code:
Sub ConcatenateSelection_SemiColon_ToClipboard()

    Dim rngCell As Range
    Dim strTemp As String
    Dim strSeparator As String
    
    'Early Binding
    'Dim objMyData As New DataObject
    'Use Tools | References to add reference
    '"Microsoft Forms 2.0" (FM20.DLL) (or later version)
    
    'Late Binding for New DataObject
    Dim objMyData As Object
    Set objMyData = CreateObject("New:{1C3B4210-F441-11CE-B9EA-00AA006B1A69}")
    'Set objMyData = CreateObject("MSFORMS.DataObject") 'Logical, but doesn't work

    strTemp = ""
    strSeparator = ";"
    
    For Each rngCell In Selection
        strTemp = strTemp & rngCell.Value
        strTemp = strTemp & strSeparator
        'rngCell.Value = ""  'Uncomment this to clear other source cells
    Next
    objMyData.SetText Left(strTemp, Len(strTemp) - Len(strSeparator))
    objMyData.PutInClipboard

End Sub

Thank you! This is super, super cool. Made my life much easier. Thanks a ton!

Ted
 
Upvote 0

Forum statistics

Threads
1,214,833
Messages
6,121,868
Members
449,053
Latest member
Mesh

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top