如何在 Excel 中將重複行移動到另一張工作表?
您的工作表可能會被重複資料堵塞,這使得有效組織和檢查資料變得具有挑戰性。本影片將逐步引導您完成查詢重複行並將其移動到另一張工作表的過程,以保持電子表格的整潔和組織性。
我們將研究利用 Excel 預建功能和功能來完成此任務的方法。無論您擁有何種程度的 Excel 技能,本課程都將為您提供成功處理重複資料和簡化工作流程的知識和技能。
將重複行移動到另一張工作表
這裡我們將首先建立一個 VBA 模組,然後選擇單元格來完成任務。讓我們來看一個簡單的過程,學習如何在 Excel 中將重複行移動到另一張工作表。
步驟 1
考慮一個 Excel 工作表,其中工作表中包含重複行,類似於下圖。
首先,右鍵單擊工作表名稱,然後選擇“檢視程式碼”以開啟 VBA 應用程式。
右鍵單擊 > 檢視程式碼。
步驟 2
然後單擊“插入”,選擇“模組”,然後將以下程式碼複製到文字框中。
插入 > 模組 > 複製。
程式碼
Sub CutDuplicates() 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:", "Move Duplicate Rows", Selection.Address, , , , , 8) If xRgS Is Nothing Then Exit Sub Set xRgD = Application.InputBox("Please select a desitination cell:", "Move Duplicate Rows", , , , , , 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
最後,選擇一個單元格來貼上資料,然後單擊“確定”。
這就是如何在 Excel 中將重複行移動到另一張工作表的方法。
注意
如果要根據行移動行,請使用以下程式碼。
程式碼
Sub CutDuplicates() 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:", "Move Duplicate Rows", Selection.Address, , , , , 8) If xRgS Is Nothing Then Exit Sub Set xRgD = Application.InputBox("Please select a desitination cell:", " Move Duplicate Rows", , , , , , 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
結論
在本教程中,我們使用了一個簡單的示例來演示如何在 Excel 中將重複行移動到另一張工作表以突出顯示特定資料集。
廣告