macros代码用于从单列中删除重复的单元格,并对其余列重复相同的单元格

我想要创buildmacros从单列中删除重复的单元格,并重复相同的其余列。

我有代码做这个单列和它的工作,但我不能够转换此代码重复其余的列相同的过程。

任何人都可以帮我这个吗?

列

Sub Delete_Dupes2() Dim lastrow As Long Application.ScreenUpdating = False With Sheet2 lastrow = .Cells(Rows.Count, 1).End(xlUp).Row .Range("$A$1:$A$" & lastrow).RemoveDuplicates Columns:=1, Header:=xlYes End With Application.ScreenUpdating = True End Sub 

这里的前10列:

 Sub Delete_Dupes3() Application.ScreenUpdating = False Dim LastRow As Long With Sheet2 For j = 1 To 10 LastRow = .Cells(.Rows.Count, j).End(xlUp).Row .Range(.Cells(1, j), .Cells(LastRow, j)).RemoveDuplicates Columns:=1, _ Header:=xlYes End If End With Application.ScreenUpdating = True End Sub 

顺便说一句,在你的截图(可能是虚拟数据),没有标题,但参数说相反: Header:=xlYes

另一种方式(R3uK张贴,当我testing我的答案)。
我已经添加了两种引用范围的方法 – 第一种返回行号,第二种返回对单元格的引用。

因为R3uK和我自己都已经放了 – 而不是使用.Range("$A$1:$A$" & lastrow)我发现它更容易使用.Range(.Cells(1, 1), .Cells(lastrow, 1))因为您使用数字而不是字母指定来引用列。

 Option Explicit Sub Test() Delete_Dupes2 1 Delete_Dupes2 3 Delete_Dupes2 ColumnNum:=5 Dim x As Long For x = 1 To 10 Delete_Dupes2 ColumnNum:=x Next x End Sub Sub Delete_Dupes2(ColumnNum As Long) Dim lastrow As Long Dim lastrow1 As Range Application.ScreenUpdating = False With Sheet2 lastrow = .Cells(Rows.Count, ColumnNum).End(xlUp).Row .Range(.Cells(1, ColumnNum), .Cells(lastrow, ColumnNum)).RemoveDuplicates Columns:=1, Header:=xlYes 'or 'Set lastrow1 = .Cells(Rows.Count, ColumnNum).End(xlUp) '.Range(.Cells(1, ColumnNum), lastrow1).RemoveDuplicates Columns:=1, Header:=xlYes End With Application.ScreenUpdating = True End Sub 

编辑:

或者您可以将过程名称更改为:
Sub Delete_Dupes2(ColumnNum As Long, wrkSht As Worksheet)

With Sheet2行更改为:
With wrkSht

将您的呼叫(testing)行更改为:

 Delete_Dupes2 1, Sheet2 Delete_Dupes2 3, Sheet1 Delete_Dupes2 ColumnNum:=5, wrkSht:=ThisWorkbook.Worksheets("AnotherSheet") Dim x As Long For x = 1 To 10 Delete_Dupes2 ColumnNum:=x, wrkSht:=ActiveWorkbook.Worksheets("ASheetInAnotherFile") Next x