How to concatenate cell values until if finds a blank cell in a column?
For example, I have a list of values which including some blank cells, and now, I want to concatenate each set of data between blanks into a cell and list the combined values in another column as following screenshot shown. How could I concatenate cells until find a blank cell in a column?
There is no direct way for you to concatenate a set of cells between blanks, but, you can apply the following VBA code to deal with it. Please do as follows:
1. Hold down the ALT + F11 keys to open the Microsoft Visual Basic for Applications window.
2. Click Insert > Module, and paste the following code in the Module Window.
VBA code: concatenate cell values until if finds a blank cell
Sub Concatenatecells() 'updateby Extendoffice Dim xRg As Range Dim xSaveToRg As Range Dim xTxt As String Dim xCell As Range Dim xTStr As String On Error Resume Next xTxt = ActiveWindow.RangeSelection.Address Set xRg = Application.InputBox("Please selecte the data range:", "Kutools for Excel", xTxt, , , , , 8) If xRg Is Nothing Then Exit Sub If xRg.Columns.Count > 1 Then MsgBox "the selected range is more than one column", vbInformation, "Kutools for Ecel" Exit Sub End If Set xSaveToRg = Application.InputBox("Please selecte output cell:", "Kutools for Excel", , , , , , 8) If xSaveToRg Is Nothing Then Exit Sub Set xSaveToRg = xSaveToRg.Cells(1) Application.ScreenUpdating = False For Each xCell In xRg If xCell <> "" Then xTStr = xTStr & xCell & " " Else xSaveToRg.Value = xTStr Set xSaveToRg = xSaveToRg.Offset(1) xTStr = "" End If Next If xTStr <> "" Then xSaveToRg.Value = Left(xTStr, Len(xTStr) - 1) Application.ScreenUpdating = True End Sub
3. Then press F5 key to run this code, and a prompt box is popped out to remind you select the data range that you want to concatenate, see screenshot:
4. Then click OK, and another prompt box is popped out, please select a cell to output the concatenated result, see screenshot:
5. And then click OK button, and the column list data are concatenated between the blanks, see screenshot:
If you have Kutools for Excel, combining its Transform Range and Combine features, you can also finish this job as soon as possible. The Transform Range utility can help you to convert the single column to a range, and the Combine feature can help you to concatenate the transformed range to a column.
|Kutools for Excel : with more than 300 handy Excel add-ins, free to try with no limitation in 30 days.|
After installing Kutools for Excel, please do as follows:
1. Select the cells that you want to concatenate, and click Kutools > Range > Transform Range, see screenshot:
2. In the Transform Range dialog box, check Single column to range option under the Transform type, and then select Blank cell delimits records under the Rows per record, see screenshot:
3. Then click Ok, and a prompt box will pop out to remind you select a cell to output the result, see screenshot:
4. Click OK, and the single column data has been converted into multiple rows and columns by the blank cell, see screenshot:
5. The converted data is in multiple columns, so you need to combine them into one column, please select the data range and click Kutools > Merge & Split > Combine Rows, Columns or Cells without Losing Data, see screenshot:
6. In the Combine Columns or Rows dialog box, do the following options:
(1.) Select Combine columns option from the To combine selected cells according to following options;
(2.) Then specify a separator that you want to separate the concatenated values;
(3.) Select how you want to deal with the combined cells. You can keep or delete the original data, and you can also merge the combined cells. See screenshot:
7. After finishing the settings, click Ok or Apply button, the cells have been concatenated into one column with a specific separator, see screenshot: