【发布时间】:2021-08-13 03:20:24
【问题描述】:
我正在尝试创建一个子程序,仅当整个行值重复时才删除重复的行(我的工作表有 20 列)。函数RemoveDuplicates Columns:=Array(1, 2), Header:=xlYes 将删除错误的行,因为我可能在所有单元格中都有重复的值,但绝不是整行。我尝试使用RemoveDuplicates Columns:=Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10), Header:=xlYes,但它给了我一个错误。所以我决定制作以下代码。代码的问题是我正在遍历所有单元格以验证任何重复的行。有没有更简单的方法来做到这一点?
谢谢!
Public Sub DeleteDupRows()
Dim plLine As Integer: plLine = 2 'sheet have header
Dim plColumn As Integer: plColumn = 1
Dim rowReferece As Integer: rowReferece = 2 'rows and columns used to search
Dim columnReference As Integer: columnReference = 1
Dim duplicated As Integer: duplicated = False
Set pl = ThisWorkbook.Worksheets("BD - Tarifas")
While pl.Cells(plLine, plColumn) <> ""
While pl.Cells(rowReferece, columnReference) <> ""
rowReferece = rowReferece + 1
duplicated = False
columnReference = 1
While pl.Cells(plLine, columnReference) = pl.Cells(rowReferece, columnReference) And pl.Cells(plLine, columnReference) <> "" 'True remains if we get through all columns
duplicated = True
columnReference = columnReference + 1
Wend
Wend
If (duplicated = True) Then pl.Cells(rowReferece, columnReference).EntireRow.Delete
plLine = plLine + 1
rowReferece = plLine
columnReference = 1
Wend
End Sub
【问题讨论】:
-
您可以逐行使用
WorksheetFunction.CountIf和WorksheetFunction.CountA并计算非空白单元格的数量是否等于与第一个单元格中的第一个单元格具有相同值的单元格的数量列。 -
Here 是我的回答之一,它描述了
RemoveDuplicates的“三个规则”。基本上,你应该使用Columns:=(VBA.Array(1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14, 15, 16, 17, 18, 19, 20))。
标签: excel vba duplicates