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.
by Allen Wyatt
(last updated March 9, 2020)
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
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.
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!
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
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
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
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.