跳到主要內容

如何串聯單元格值,直到在列中找到空白單元格為止?

doc 連接直到空白 1

例如,我有一個包含一些空白單元格的值列表,現在,我想將空白之間的每組數據連接到一個單元格中,並在另一列中列出組合值,如下圖所示。 如何連接單元格直到在列中找到空白單元格?

使用 VBA 代碼連接單元格值,直到在列中找到空白單元格

連接儲存格值,直到使用 Kutools for Excel 在列中找到空白儲存格


箭頭藍色右氣泡 使用 VBA 代碼連接單元格值,直到在列中找到空白單元格

沒有直接的方法可以連接空白之間的一組單元格,但是,您可以應用以下 VBA 代碼來處理它。 請按以下步驟操作:

1。 按住 ALT + F11 鍵打開 Microsoft Visual Basic for Applications窗口.

2。 點擊 插入 > 模塊,然後將以下代碼粘貼到 模塊窗口.

VBA代碼:連接單元格值,直到找到空白單元格

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. 然後按 F5 鍵運行此代碼,會彈出提示框提醒您選擇要連接的數據范圍,見截圖:

doc 連接直到空白 10

4。 然後點擊 OK,又彈出一個提示框,請選擇一個單元格輸出拼接結果,見截圖:

doc 連接直到空白 11

5. 然後點擊 OK 按鈕,列列表數據在空白之間連接,參見屏幕截圖:

doc 連接直到空白 1


箭頭藍色右氣泡 連接儲存格值,直到使用 Kutools for Excel 在列中找到空白儲存格

如果你有 Excel的Kutools,結合 變換範圍結合 功能,您也可以盡快完成這項工作。 這 變換範圍 實用程序可以幫助您將單列轉換為範圍,並且 結合 功能可以幫助您將轉換後的範圍連接到列。

Excel的Kutools : 帶有300多個便捷的Excel加載項,可以在30天內免費試用. 

安裝後 Excel的Kutools,請執行以下操作:

1. 選擇要連接的單元格,然後單擊 庫工具 > 範圍 > 變換範圍,請參見屏幕截圖:

2。 在 變換範圍 對話框,檢查 單列到範圍 選項下 轉換類型,然後選擇 空白單元格分隔記錄 每條記錄行,請參見屏幕截圖:

doc 連接直到空白 3

3. 然後點擊 Ok,會彈出提示框提醒您選擇輸出結果的單元格,見截圖:

doc 連接直到空白 4

4。 點擊 OK,單列數據已被空白單元格轉換為多行多列,見截圖:

doc 連接直到空白 5

5。 轉換後的數據為多列,需要合併為一列,請選擇數據范圍,點擊 庫工具 > 合併與拆分 > 合併行,列或單元格而不會丟失數據,請參見屏幕截圖:

6。 在 合併列或行 對話框中,執行以下選項:

(1.)選擇 合併欄 選項從 根據以下選項合併選定的單元格;

(2.) 然後指定一個分隔符來分隔連接的值;

(3.) 選擇您想要如何處理組合單元格。 您可以保留或刪除原始數據,也可以合併合併的單元格。 看截圖:

doc 連接直到空白 7

7。 完成設置後,單擊 Ok or 登記 按鈕,單元格已連接成具有特定分隔符的一列,請參見屏幕截圖:

doc 連接直到空白 8

立即下載和免費試用Excel的Kutools!

最佳辦公生產力工具

🤖 Kutools 人工智慧助手:基於以下內容徹底改變數據分析: 智慧執行   |  生成代碼  |  建立自訂公式  |  分析數據並產生圖表  |  呼叫 Kutools 函數...
熱門特色: 尋找、突出顯示或識別重複項   |  刪除空白行   |  合併列或儲存格而不遺失數據   |   沒有公式的回合 ...
超級查詢: 多條件VLookup    多值VLookup  |   跨多個工作表的 VLookup   |   模糊查詢 ....
高級下拉列表: 快速建立下拉列表   |  依賴下拉列表   |  多選下拉列表 ....
欄目經理: 新增特定數量的列  |  移動列  |  切換隱藏列的可見性狀態  |  比較範圍和列 ...
特色功能: 網格焦點   |  設計圖   |   大方程式酒吧    工作簿和工作表管理器   |  資源庫 (自動文字)   |  日期選擇器   |  合併工作表   |  加密/解密單元格    按清單發送電子郵件   |  超級濾鏡   |   特殊過濾器 (過濾粗體/斜體/刪除線...)...
前 15 個工具集12 文本 工具 (添加文本, 刪除字符,...)   |   50+ 圖表 類型 (甘特圖,...)   |   40+ 實用 公式 (根據生日計算年齡,...)   |   19 插入 工具 (插入二維碼, 從路徑插入圖片,...)   |   12 轉化 工具 (數字到單詞, 貨幣兌換,...)   |   7 合併與拆分 工具 (高級合併行, 分裂細胞,...)   |   ... 和更多

使用 Kutools for Excel 增強您的 Excel 技能,體驗前所未有的效率。 Kutools for Excel 提供了 300 多種進階功能來提高生產力並節省時間。  點擊此處獲取您最需要的功能...

產品描述


Office選項卡為Office帶來了選項卡式界面,使您的工作更加輕鬆

  • 在Word,Excel,PowerPoint中啟用選項卡式編輯和閱讀,發布者,Access,Visio和Project。
  • 在同一窗口的新選項卡中而不是在新窗口中打開並創建多個文檔。
  • 將您的工作效率提高 50%,每天為您減少數百次鼠標點擊!
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
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations