There may be times when you have a need to concatenate cells together. For instance, you may have information in three columns, but you want it combined together into the first column of each row. The following macro, StuffTogether, will do just that. It examines the range of cells you select, and then moves everything from each cell in a row into the first cell of the row.
Sub StuffTogether() Dim FirstCol As Integer, FirstRow As Integer Dim ColCount As Integer, RowCount As Integer Dim ThisCol As Integer, ThisRow As Integer Dim J As Integer, K As Integer Dim MyText As String FirstCol = ActiveWindow.RangeSelection.Column FirstRow = ActiveWindow.RangeSelection.Row ColCount = ActiveWindow.Selection.Columns.Count RowCount = ActiveWindow.Selection.Rows.Count For J = 1 To RowCount ThisRow = FirstRow + J - 1 MyText = "" For K = 1 To ColCount ThisCol = FirstCol + K - 1 MyText = MyText & Cells(ThisRow, ThisCol).Text & " " Cells(ThisRow, ThisCol).Value = "" Next K MyText = Trim(MyText) Cells(ThisRow, FirstCol).Value = MyText Next J End Sub
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11944) applies to Microsoft Excel 2007, 2010, 2013, and 2016. You can find a version of this tip for the older menu interface of Excel here: Combining Columns.
Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!
The Clipboard is essential to move or copy information from one place in Excel to another. If you get an error when you ...
Discover MoreUnder the right circumstances, you may notice problems when copying dates from one workbook to another. This tip explains ...
Discover MoreWant a quick way to tell how may rows and columns you've selected? Here's what I do when I need to know that information.
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2020-03-10 15:44:24
Jake
@Gerhard Schweizer could not agree more. The formula for combining A1, B1 and C1 with a dash between each would be
=A1&"-"&B1&"-"&C1
All three now appear and other things can be added as well instead of a dash. I have a lot of titles for sections that pull dates and text to form a descriptive heading. I have not have much use for Concatenate once I learned the & method.
2020-03-09 18:12:18
robert
My hat is off to you Willie. Finally, an expert has taken the time to explain the procedure so even the newest Excel user can apprehend. I must admit, I follow the Tips article mostly to catch you and Rick give the best solutions. Thank you.
2016-05-19 07:55:51
Willy Vanhaelen
@Ray J
You have a point. My solution is rather "high-tech VBA" and not simple in the sense of easy to understand. it's working is probably not obvious for novices. I give hereafter a full explanation of how it works, so more experienced users can perhaps learn something.
But even if you stick with the row by row and then cell by cell solution used in this tip's macro, you can drastically simplify it:
Sub StuffTogether()
Dim cell As Range, Ro As Range, S As String
For Each Ro In Selection.Rows
S = ""
For Each cell In Ro.Cells
S = S & cell.Value & " "
Next cell
Ro.ClearContents
Ro.Cells(1) = Trim(S)
Next Ro
End Sub
If you read this code, it is almost self explanatory even for novices.
This was also my first approach but then my intuition told me that VBA's Join function was ideal for joining (concatenate) the cells of each row in one go (see the macro in my post of 15 May).
VBA's help says: JOIN returns a string created by joining a number of substrings contained in an array.
Syntax: Join(sourcearray[, delimiter]). If you omit the delimiter Join uses a space by default.
Join expects a 2D array as argument. The cells of a row however are a 3D array but Excel's Index method translates it to a 2D array as expected by Join.
2016-05-16 18:49:37
John Jacobson
I agree with Dean, a User-Defined Function works best for me. I've had my own version for years, almost identical to Dean's below. Advantages:
1. can use any string as a delimiter
2. doesn't destroy the original values
3. doesn't add delimiter for blank cells in the range
4. can use non-contiguous cells. Using Dean's UDF below (with "-" as a delimiter), this would look like =ConcatAll((A2, C2, G2),"-") where a contiguous cell range would look like =ConcatAll(A2:D2,"-").
2016-05-16 08:37:27
Ray J
Fewer lines of code, Willy, but not necessarily simpler (nor intuitive) for the novices that might visit this page.
2016-05-15 12:41:29
Willy Vanhaelen
Simplicity is the ultimate sophistication (Leonardo da Vinci). This tiny macro does the job of the jumbo one in this tip equally well and faster of course:
Sub StuffTogether()
Dim Ro As Range, S As String
For Each Ro In Selection.Rows
S = Join(Application.Index(Ro.Value, 1, 0))
Ro.ClearContents
Ro.Cells(1) = S
Next Ro
End Sub
The kernel of this macro is the combination of VBA's Join function and the array version of Excel's Index method. This one line code does the same work as the 9 lines of the For Next J loop in this tip's macro.
2016-05-14 12:58:30
Bill
When you work with a program you assume everyone understands the Language/procedure as laid out but we all don't I would recommend that you use screen shots with the descriptions for novices like me, Thanks Bill
2016-05-14 12:54:28
Dean Cardno
I found it easier to do with a UDF. If I need to make it permanent I just paste the results onto themselves as values. The stSep argument allows me to specify a separator - semi-colons for e-mail addresses for example. If I don't need one I just use a space
Function ConcatAll(rgVals as Range, stSep as String) as String
Dim cl as Range
For Each cl in rgVals
If cl.Value <> "" Then
ConcatAll = ConcatAll & cl.Value & stSep
End If
Next cl
ConcatAll = Left(ConcatAll, Len(ConcatAll) - Len(stSep))
End Function
2016-05-14 08:06:07
Gerhard Schweizer
I have always asked myself why Microsoft does not offer an improved version of function CONCATENATE. This function allows just single cells now, and it is rather obsolete since ampersands (&) do the same job. In the improved version, it should be possible to enter one or more arrays (ranges), along with an optional parameter = separator to appear between each element. The new version would even be downward compatible with the old one.
Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
Copyright © 2021 Sharon Parq Associates, Inc.
Comments