Please Note: This article is written for users of the following Microsoft Excel versions: 2007, 2010, 2013, and 2016. If you are using an earlier version (Excel 2003 or earlier), this tip may not work for you. For a version of this tip written specifically for earlier versions of Excel, click here: Combining Columns.

Combining Columns

by Allen Wyatt
(last updated March 9, 2020)

9

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:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

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.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Setting the AutoRecover Directory

Excel, by default, periodically writes information to AutoRecover files that can help protect your data in case Excel is ...

Discover More

Deleting Tab Stops

Need to delete some tabs tops in a paragraph? It's easy to do using the Tabs dialog box, as described in this tip.

Discover More

Table Won't Flow to Second Page

Have you ever created a table that never seems to flow to a second page, even when it should? This could be caused by any ...

Discover More

Comprehensive VBA Guide Visual Basic for Applications (VBA) is the language used for writing macros in all Office programs. This complete guide shows both professionals and novices how to master VBA in order to customize the entire Office suite for their needs. Check out Mastering VBA for Office 2010 today!

More ExcelTips (ribbon)

Merging Cells to a Single Sum

One way to make your worksheets less complex is to get rid of detail and keep only the summary of that detail. Here's how ...

Discover More

Easily Dividing Values by 1000

Sometimes the data in a worksheet isn't in the exact format desired. If you want to divide your values by 1,000, there ...

Discover More

Automatically Breaking Text

Want to convert the text in a cell so that it wraps after every word? You could edit the cell and press Alt+Enter after ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is 6 + 3?

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.


This Site

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.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.