Skip to main content

How to concatenate cell values until if finds a blank cell in a column?

Author: Xiaoyang Last Modified: 2020-07-31
doc concatenate until blank 1

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?

Concatenate cell values until if finds a blank cell in a column with VBA code

Concatenate cell values until if finds a blank cell in a column with Kutools for Excel


arrow blue right bubble Concatenate cell values until if finds a blank cell in a column with VBA code

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:

doc concatenate until blank 10

4. Then click OK, and another prompt box is popped out, please select a cell to output the concatenated result, see screenshot:

doc concatenate until blank 11

5. And then click OK button, and the column list data are concatenated between the blanks, see screenshot:

doc concatenate until blank 1


arrow blue right bubble Concatenate cell values until if finds a blank cell in a column with Kutools for Excel

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:

doc concatenate until blank 3

3. Then click Ok, and a prompt box will pop out to remind you select a cell to output the result, see screenshot:

doc concatenate until blank 4

4. Click OK, and the single column data has been converted into multiple rows and columns by the blank cell, see screenshot:

doc concatenate until blank 5

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:

doc concatenate until blank 7

7. After finishing the settings, click Ok or Apply button, the cells have been concatenated into one column with a specific separator, see screenshot:

doc concatenate until blank 8

Download and free trial Kutools for Excel Now !

Best Office Productivity Tools

🤖 Kutools AI Aide: Revolutionize data analysis based on: Intelligent Execution   |  Generate Code  |  Create Custom Formulas  |  Analyze Data and Generate Charts  |  Invoke Kutools Functions
Popular Features: Find, Highlight or Identify Duplicates   |  Delete Blank Rows   |  Combine Columns or Cells without Losing Data   |   Round without Formula ...
Super Lookup: Multiple Criteria VLookup    Multiple Value VLookup  |   VLookup Across Multiple Sheets   |   Fuzzy Lookup ....
Advanced Drop-down List: Quickly Create Drop Down List   |  Dependent Drop Down List   |  Multi-select Drop Down List ....
Column Manager: Add a Specific Number of Columns  |  Move Columns  |  Toggle Visibility Status of Hidden Columns  |  Compare Ranges & Columns ...
Featured Features: Grid Focus   |  Design View   |   Big Formula Bar    Workbook & Sheet Manager   |  Resource Library (Auto Text)   |  Date Picker   |  Combine Worksheets   |  Encrypt/Decrypt Cells    Send Emails by List   |  Super Filter   |   Special Filter (filter bold/italic/strikethrough...) ...
Top 15 Toolsets12 Text Tools (Add Text, Remove Characters, ...)   |   50+ Chart Types (Gantt Chart, ...)   |   40+ Practical Formulas (Calculate age based on birthday, ...)   |   19 Insertion Tools (Insert QR Code, Insert Picture from Path, ...)   |   12 Conversion Tools (Numbers to Words, Currency Conversion, ...)   |   7 Merge & Split Tools (Advanced Combine Rows, Split Cells, ...)   |   ... and more

Supercharge Your Excel Skills with Kutools for Excel, and Experience Efficiency Like Never Before. Kutools for Excel Offers Over 300 Advanced Features to Boost Productivity and Save Time.  Click Here to Get The Feature You Need The Most...

Description


Office Tab Brings Tabbed interface to Office, and Make Your Work Much Easier

  • Enable tabbed editing and reading in Word, Excel, PowerPoint, Publisher, Access, Visio and Project.
  • Open and create multiple documents in new tabs of the same window, rather than in new windows.
  • Increases your productivity by 50%, and reduces hundreds of mouse clicks for you every day!
Comments (8)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hi, I am using the VBA code, but instead of putting the data into separate rows, it is putting it all in a single cell. Does anyone have an idea why it is doing this?
This comment was minimized by the moderator on the site
Hi! After changing the values (ex, watermelon instead of apple) why is the formula not working?
This comment was minimized by the moderator on the site
Hello, Cristina

Our article provides VBA code, doesn't provide a formula. If you change the cell content, you must re-run the code again to get the correct result.
Thank you!
This comment was minimized by the moderator on the site
great job bro, Very useful
This comment was minimized by the moderator on the site
Hello,
You are welcome. Glad it helps. Any questions, please feel free to contact us. Have a great day.
Sincerely,
Mandy
This comment was minimized by the moderator on the site
It shows there's a syntax error on line 3 "Dim xRg As Range" when I'm trying to run this module based on method 1, what should I do about it?
This comment was minimized by the moderator on the site
Hello, JJ, The code works well in my worksheet, which Excel version do you use?Or you can insert a screesnhot of your problem here.
This comment was minimized by the moderator on the site
How would you do it if you want to base it off another column? I have similar data, but column B is a number then notes(like your fruit) in column I, I want to combine all notes in column I until there is another cell in Column B that has only number. Sometimes there are letter under B or the cells are blank which is why it needs to combine until a number. Then finally can it output back to where it started the combine? IE all notes for first number are in column j, but if the next number is 5 rows below that is fine, it just goes in column j but same row number as the number is found?
There are no comments posted here yet
Leave your comments
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations