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 May 14, 2016)

7

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

Entering Tabs in a Table

When press the Tab key while entering info into a table, Word dutifully moves to the next table cell. If you don't want ...

Discover More

Fast Spelling Corrections

Want to correct the spelling of a word that Word thinks is improperly spelled? A quick way to do it is to right-click the ...

Discover More

Saving Custom Formats

While the implementation of custom formats in Excel is not terribly robust, you can still achieve some amazing results ...

Discover More

Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!

More ExcelTips (ribbon)

Concatenating Ranges of Cells

Putting the contents of two cells together is easy. Putting together the contents of lots of cells is more involved, as ...

Discover More

Typing Check Marks into Excel

Need to enter a check mark into a cell? There are a number of ways you can get the desired character, depending on the ...

Discover More

Displaying Row and Column Labels

When you create a worksheet, it is common to place headings at the top of each column and the left of each row so you can ...

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 5 + 3?

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.