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中将重复的行移动到另一个工作表中,以突出显示特定的数据集。