跳到主要內容

如何在Excel中基於重複項將多行合併為一行?

也許您有以下屏幕截圖數據,在“產品名稱”列A中,有一些重複項,現在您需要刪除列A中的重複項,但要合併列B中的相應值,請參見屏幕截圖:

文檔高級組合1 -2 文檔高級組合2

您有解決此任務的有效方法嗎?

使用VBA代碼將多個重複的行合併為一個

將多個重複的行合併為一個Kutools for Excel

合併多個重複的行並使用Kutools for Excel將值求和/取平均值


箭頭藍色右氣泡使用VBA代碼將多個重複的行合併為一個

下面的VBA代碼可以幫助您根據列A的值合併重複的行,並且下面的代碼僅適用於兩列。 因此,如果您有兩列數據,那麼此代碼將為您提供幫助。

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

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

VBA代碼:將多行合併為一

Sub CombineRows()
'Update 20131202
Dim WorkRng As Range
Dim Dic As Variant
Dim arr As Variant
On Error Resume Next
xTitleId = "KutoolsforExcel"
Set WorkRng = Application.Selection
Set WorkRng = Application.InputBox("Range", xTitleId, WorkRng.Address, Type:=8)
Set Dic = CreateObject("Scripting.Dictionary")
arr = WorkRng.Value
For i = 1 To UBound(arr, 1)
    xvalue = arr(i, 1)
    If Dic.Exists(xvalue) Then
        Dic(arr(i, 1)) = Dic(arr(i, 1)) & " " & arr(i, 2)
    Else
        Dic(arr(i, 1)) = arr(i, 2)
    End If
Next
Application.ScreenUpdating = False
WorkRng.ClearContents
WorkRng.Range("A1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.keys)
WorkRng.Range("B1").Resize(Dic.Count, 1) = Application.WorksheetFunction.Transpose(Dic.items)
Application.ScreenUpdating = True
End Sub

3。 然後按 F5 鍵,並彈出一個提示框,提醒您選擇需要組合的範圍。 看截圖:

文檔高級組合3

4。 然後點擊 OK,則B列中的值已合併為一個記錄,並用空格分隔。

文檔高級組合4

筆記:

1.此代碼可以銷毀原始數據,使用時最好先備份數據。

2.此代碼僅適用於兩列,如果您有兩列以上,則此代碼將僅合併前兩列並刪除其他列數據。 使用此代碼時,請務必謹慎。


箭頭藍色右氣泡將多個重複的行合併為一個Kutools for Excel

既然上面的代碼有一定的局限性和風險,是否有方便而安全的方法來完成這項工作? 用 Excel的Kutools高級合併行 實用程序,您可以快速刪除您所基於的列中的多個重複行,然後合併或對其他記錄進行一些計算到適當的單元格中。

Excel的Kutools 包括300多個便捷的Excel工具。 30天免費試用,不受限制。 立即獲取.

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

1。 選擇要合併重複項的範圍數據。

文檔高級組合5

2。 然後點擊 庫工具 > 內容轉換器 > 高級合併行。 看截圖:

文檔高級組合6

3。 在 高級合併行 對話框中,選擇列名稱作為要基於其組合值的鍵列,然後單擊“確定”。 關鍵列。 看截圖:

文檔高級組合7

4。 然後繼續單擊您需要將其值合併到一條記錄中的列,然後單擊 合併作業 並選擇 僅與分隔符結合,然後選擇一個分隔符,您需要從右邊的列表中分隔出組合的值。 看截圖:

文檔高級組合8

5。 完成設置後,單擊 OK,您的數據將根據鍵列進行合併,並以分隔符分隔。

文檔高級組合9

注意: 如果您不需要為其餘列設置操作,則只需保留默認狀態即可。


箭頭藍色右氣泡合併多個重複的行並使用Kutools for Excel將值求和/取平均值

有了這個多功能 高級合併行 實用程序,您可以合併重複的行,並對值進行一些計算。

1。 選擇要合併重複項的範圍數據。

2。 去的 高級合併行 對話框;

  • (1.)選擇一個列名作為要基於其計算其他列值的鍵列,然後單擊 關鍵列;
  • (2.)選擇您需要對值進行一些計算的列,然後單擊 合併作業 > 合併計算,然後選擇一種所需的計算類型,在這種情況下,我選擇 總和。 看截圖:

文檔高級組合10

3。 如果您不需要為其餘列設置操作,只需保持默認狀態,然後單擊 OK,則A列中的重複值將被合併,並且B列中其對應的值將相加。 查看屏幕截圖:

文檔高級組合11 -2 文檔高級組合12

筆記:

1.如果您檢查 我的數據有標題 選項,應用此實用程序時將忽略範圍的標題(第一行)。

2. 使用格式化的值:合併重複的單元格後,將保留值的格式。

3.此實用程序不可用於過濾數據。

單擊以了解有關此高級合併行功能的更多信息。


相關文章:

合併重複的行並求和

最佳辦公生產力工具

🤖 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
I have used the code above successfully in the past however today, it is deleting column B. Looks like it's just removing duplicates...not sure what the issue is.

Sample Data
Reece, Ashley G01-Registered-UWG
Reece, Chris G02-Registered-KSU
Reece, Chris G03-Dropped-KSU
Reece, Jayden G02-Registered-UNG
Reece, Jayden G02-Withdrawn-UNG
This comment was minimized by the moderator on the site
I have in column A

1 Cast
2 Crew
3 Hiring
4 Cast
5 Hiring
6 Crew

I need these unique CAST CREW AND HIRING in another column without delete in column A entries.

Ex: I need (after formula)

1 Cast
2 Crew
3 Hiring

Is it possible by formula?
This comment was minimized by the moderator on the site
Above Macro only Combine the limited cell(e.g 20 cells) in b column. How to Combine more cells
This comment was minimized by the moderator on the site
Same - 2021 and this still only works if you have like 20 rows...
This comment was minimized by the moderator on the site
I've found that the "Combine multiple duplicate rows and sum/average the values" feature within KUTools does not work if the numbers in the row have a comma or period. This makes the tool unusable for combining and summing currency fields (which is why I tried the product in first place). I'm currently using the trial version which states that it is full-functioned; is it possible that this works in the paid version?
This comment was minimized by the moderator on the site
Same here. I was about to purchase because the tools are very nice, but figured I would try it first. Combine never works. I tried it for about 3 weeks now and it always locks up. No matter the power of my computer (ran on my at home powerhouse computer, same issue)
I kept cutting down to less and less data to see if it would work but it still would not.
The last one I tried was 4 columns and 100k rows and it still locked up. Not very useful unless you have like < 100 rows or something.
This comment was minimized by the moderator on the site
Same here, I filtered my data all the way down to 4 columns and 100k rows and it still work not work. I have tried this for 3 weeks now and unable to successfully complete my task. Kutools will only work if you have very few rows.
This comment was minimized by the moderator on the site
[quote]I've found that the "Combine multiple duplicate rows and sum/average the values" feature within KUTools does not work if the numbers in the row have a comma or period. This makes the tool unusable for combining and summing currency fields (which is why I tried the product in first place). I'm currently using the trial version which states that it is full-functioned; is it possible that this works in the paid version?By David Greene[/quote] Sorry, we will fix it in next version.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations