跳到主要內容

如何將重複的行移動到Excel中的另一個工作表?

現在,如果您有一個數據范圍列表,其中包含一個特定列中的某些重複值,那麼現在,您要根據重複單元格將整個行移動到另一張工作表中。 您如何在Excel中處理此任務?

根據一列中的重複單元格將整行移動到另一張工作表

根據重複的行將整個行移動到另一張工作表


根據一列中的重複單元格將整行移動到另一張工作表

如果一列中有重複的值,則將整行移動到另一張工作表中,以下VBA代碼可以幫您一個忙:

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

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

VBA代碼:根據一列中的重複單元格將整行移動到另一張工作表:

Sub CutDuplicates()
'Updateby Extendoffice
    Dim xRgS As Range
    Dim xRgD As Range
    Dim I As Long, J As Long
    On Error Resume Next
    Set xRgS = Application.InputBox("Please select the column:", "KuTools For Excel", Selection.Address, , , , , 8)
    If xRgS Is Nothing Then Exit Sub
    Set xRgD = Application.InputBox("Please select a desitination cell:", "KuTools For Excel", , , , , , 8)
    If xRgD Is Nothing Then Exit Sub
    xRows = xRgS.Rows.Count
    J = 0
    For I = xRows To 1 Step -1
        If Application.WorksheetFunction.CountIf(xRgS, xRgS(I)) > 1 Then
            xRgS(I).EntireRow.Copy xRgD.Offset(J, 0)
            xRgS(I).EntireRow.Delete
            J = J + 1
        End If
    Next
End Sub

3。 然後按 F5 鍵以運行此代碼,然後在彈出的對話框中選擇包含要基於其移動的重複單元格的列,請參見屏幕截圖:

4。 然後點擊 OK,在另一個提示框中,請在另一個工作表中選擇一個單元格,您要在其中放置移動的行,請參見屏幕截圖:

5。 然後點擊 OK,則A列中具有重複值的行已移至新工作表,請參見屏幕截圖:


根據重複的行將整個行移動到另一張工作表

如果要移動單元格區域中的重複行,以下VBA代碼可以為您提供幫助:

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

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

VBA代碼:根據重複的行將整個行移動到另一張工作表:

Sub CutDuplicates()
'Updateby Extendoffice
    Dim xRgD As Range, xRgS As Range
    Dim I As Long, J As Long, K As Long, KK As Long
    On Error Resume Next
    Set xRgS = Application.InputBox("Please select the data range:", "KuTools For Excel", Selection.Address, , , , , 8)
    If xRgS Is Nothing Then Exit Sub
    Set xRgD = Application.InputBox("Please select a desitination cell:", "KuTools For Excel", , , , , , 8)
    If xRgD Is Nothing Then Exit Sub
    KK = 0
    For I = xRgS.Rows.Count To 1 Step -1
        For J = 1 To I - 1
            For K = 1 To xRgS.Columns.Count
                Debug.Print xRgS.Rows(I).Cells(, K).Value
                Debug.Print xRgS.Rows(J).Cells(, K).Value
                If xRgS.Rows(I).Cells(, K).Value <> xRgS.Rows(J).Cells(, K).Value Then Exit For
            Next
            If K = xRgS.Columns.Count + 1 Then
                xRgS.Rows(I).EntireRow.Copy xRgD.Offset(KK, 0).EntireRow
                xRgS.Rows(I).EntireRow.Delete
                KK = KK + 1
            End If
        Next
    Next
End Sub

3。 然後按 F5 鍵以運行此代碼,在彈出的對話框中,選擇要移動重複行的數據范圍,請參見屏幕截圖:

4。 點擊 OK 按鈕,然後在另一個提示框中,在新工作表中選擇一個單元格,您要在其中放置已移動的重複行,請參見屏幕截圖:

5。 然後點擊 OK 按鈕,現在,重複的行已被移到您一次指定的另一個工作表中,請參見屏幕截圖:

最佳辦公生產力工具

🤖 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 (6)
No ratings yet. Be the first to rate!
This comment was minimized by the moderator on the site
Hola buenas,

Tenia una pregunta con respecto a esta macro; Esta macro es exactamente lo que necesito y funciona perfectamente, pero mi problema es que solo funciona en grupo de datos que no sean muy grandes.

Cuando la aplico sobre un grupo de datos grandes (en mi caso, 50mil filas), excel inevitablemente colapsa; He probado muchas cosas pero sigue colapsando.

Hay alguna manera de hacer que la macro funcione en estos casos? alguna idea? Gracias de antemano.

Rafael.
This comment was minimized by the moderator on the site
Hello, Rafael

Yes, as you said, if the data is large, the vba code will not work perfectly. In this case, you can use the auxiliary column to mark the duplicate row first, then use the filter function to filter out the duplicate row, finally copy the duplicate row to another worksheet. Please see the below demo:
To find the duplicate rows, please apply this formula: =IF(COUNTIFS($A$2:$A2,$A2,$B$2:$B2,$B2,$C$2:$C2,$C2) >1, "Duplicate row", "")
If you just want to find the duplicate cells in one column, please apply this formula: =IF(COUNTIFS($A$2:$A2,$A2) >1, "Duplicate row", "")
https://www.extendoffice.com/images/stories/comments/comment-skyyang/move-duplicates.gif
Please have a try, hope it can help you!
This comment was minimized by the moderator on the site
Hola, que tal,

Tengo una duda con respecto a esta macro; La macro funciona perfectamente y es justo lo que necesito, el problema es que habitualmente necesito utilizarla con tablas de excel en las que hay como mas de 50 mil entradas.

Al utilizarla, Excel inevitablemente se colapsa y no responde.

Hay alguna manera de conseguir que esta macro funcione en estos casos? O quizas, conseguir alguna formula/combinacion de formulas en excel que haga lo mismo? Lo comento porque en el caso de las formulas, normalmente excel no suele tardar tanto en procesar y lo resuelve mas rapido.

Un saludo, y gracias.

Rafael.
This comment was minimized by the moderator on the site
For some reason. This worked once, but I cant run it again. IT keeps timing out. Any tips?
This comment was minimized by the moderator on the site
It's working bro!!!!! Thanks.
This comment was minimized by the moderator on the site
This is exactly what I was looking for, thank you! I did it wrong the first few times and then it kept freezing. But once I did it exactly like the screenshots (selecting the exact range, copy/pasting the headers in the other sheet, and only selecting the first field under the first header) the macros was instantaneous.
There are no comments posted here yet
Please leave your comments in English
Posting as Guest
×
Rate this post:
0   Characters
Suggested Locations