Written by Allen Wyatt (last updated November 11, 2022)
This tip applies to Excel 2007, 2010, 2013, and 2016
Bonnie described a common problem that occurs when importing a file into Excel. The file being imported is a scanned text file, and the import goes just fine, with one small glitch: in one column where there was wrapped text in the original document, the text now occupies several rows in the worksheet. Bonnie is looking for a way to combine those rows back into a single cell in that column.
There are a couple of ways this can be done. If you don't have to do this too often, a formulaic approach may be best. Just use the ampersand (&) to concatenate the contents of the rows you want to combine:
=C6 & " " & C7 & " " & C8 & " " & C9
The result is all the text combined into a single cell. You can copy this result to the Clipboard and use Paste Special to put it into the final cell where you need it. Finally, you can delete the original multiple rows that are no longer needed.
Another approach works very well if the cells you want to combine all contain text. Let's say you want to combine cells C6:C9. All you need to do is to widen column C so it could contain the contents of those cells on a single line. Then, select the cells. Display the Home tab of the ribbon and note that there is a tool in the Editing group called Fill. If you click the tool you see a drop-down list of options from which you should choose Justify. When you do, you end up with all the values in cell C6, separated by spaces.
If you need to concatenate cells quite often, you may benefit from a simple macro:
Sub Combine() Dim J As Integer If Selection.Cells.Count > 1 Then For J = 2 To Selection.Cells.Count Selection.Cells(1).Value = _ Selection.Cells(1).Value & " " & _ Selection.Cells(J).Value Selection.Cells(J).Clear Next J End If End Sub
To use this macro, select the cells you want to concatenate and then run the macro. The contents of all the cells are combined into the first cell in the selection, then whatever is in the other cells is cleared. The macro doesn't delete any rows; that is left for you to do. It does, however, combine the contents quickly—even more quickly if you assign a shortcut key to the macro.
Note:
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (11496) 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 Multiple Rows in a Column.
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!
Need to enter the current time into a cell? It's easy to do using this keyboard shortcut. The shortcut is a handy one to ...
Discover MoreExcel allows you to easily paste information into a worksheet, including through simply dragging and dropping the ...
Discover MoreAutoComplete can help you to more quickly enter information in a worksheet. How it works, behind the scenes, can affect ...
Discover MoreFREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
2023-01-30 10:12:42
J. Woolley
The Tip mentions Home > Editing > Fill > Justify. That feature is rather obscure, but My Excel Toolbox includes the FillJustifyText macro to make it less so.
First the macro displays information in a MsgBox (see Figure 1 below)
Then the macro uses Application.InputBox to define a Range (see Figure 2 below)
Next the macro saves content of the Range plus 100 rows directly below it to prepare for Undo (because running a macro always empties the Undo stack).
Finally the macro applies the Range.Justify method described here:
https://learn.microsoft.com/en-us/office/vba/api/excel.range.justify
See https://sites.google.com/view/MyExcelToolbox
And for more on this subject, see the following:
https://excelchamps.com/excel-basics/fill-justify
https://www.myonlinetraininghub.com/who-needs-word-when-excel-has-got-fill-justify-text
https://www.theexceladdict.com/tips/151115-Instead-Of-Using-Wrap-Text,-Justify-Text-On-Multiple-Rows.html
Figure 1.
Figure 2.
2022-11-11 09:43:18
Alan Cannon
2 comments:
(1) don't forget to turn on "Wrap Text" for the destination either before or after the combining, or
(2) replace the blank character " " with an ASCII line feed
2017-06-30 03:00:23
Stephen
Ah yes, sorry. I tried to do it for columns rather than rows. That doesn't work!
2017-06-29 03:00:52
Shreepad SM Gandhi
Thanks Allen
Wasn't aware that Fill-Justify could also be used this way. Tried it. Nice.
Stephen,
Even I use Excel 2010. It worked well for me. Am wondering why you couldn't succeed. Suggest to follow just the way explained in this tip. Hope it works.
Andrew,
I guess Excel 2010 doesn't has this Excel function TEXTJOIN. However I do have Excel 2013 on my another machine. Shall try that for sure. Thanks for sharing.
2017-06-24 08:52:40
Stephen
"Another approach works very well...." This simply does not work for me using Excel 2010.
2017-06-24 05:42:33
Andrew
There is a new function in Excel, TEXTJOIN, available in the latest version of Excel.
Allen's formula above could be replaced with
=TEXTJOIN(" ",TRUE,C6:C9)
The first argument is the delimiter you wish to use, and the second relates to how empty cells are treated.
More detail is at https://support.office.com/en-us/article/TEXTJOIN-function-357b449a-ec91-49d0-80c3-0e8fc845691c.
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 © 2023 Sharon Parq Associates, Inc.
Comments