我有一张看起来像这样的表:
WebDocumentId To remove
675082 0
675082 0
675082 0
675083 0
675083 1
675083 0
675083 0
675083 0
675085 1
675085 0
675087 0
675087 0
675087 1
675087 0
675089 0
675089 0
675089 0
675089 0
我的目的是以某种方式删除所有相同的条目,其中1个在“要删除”列中至少出现一次。如果1是id的第一个值,则第三列上的简单VLOOKUP将起作用。
总结一下,决赛桌应如下所示:
WebDocumentId To remove
675082 0
675082 0
675082 0
675089 0
675089 0
675089 0
675089 0
提前致谢!
答案 0 :(得分:0)
见下文。关于此代码的一些重要说明:
它会删除整行,而不只是列A和A列。 B.如果你有 列C或更高的数据,将删除该数据。请用 小心。的
Option Explicit
Sub RemoveEntries()
Dim ws As Worksheet
Set ws = ThisWorkbook.Worksheets("Sheet1")
Dim startRow As Long
startRow = 2
Dim endRow As Long
endRow = ws.UsedRange.Rows.Count
' make a list of IDs to delete
Dim rowsToDelete() As String
Dim arraySize As Long
arraySize = -1 ' note this isn't zero
Dim row As Long
For row = startRow To endRow
If ws.Cells(row, 2).Value = 1 Then
arraySize = arraySize + 1
ReDim Preserve rowsToDelete(arraySize)
rowsToDelete(arraySize) = ws.Cells(row, 1)
End If
Next row
' delete the rows
Dim checkRow As Long
For row = endRow To startRow Step -1 ' note that we're starting at the bottom
For checkRow = 0 To UBound(rowsToDelete)
If ws.Cells(row, 1).Value = rowsToDelete(checkRow) Then
ws.Rows(row).Delete
Exit For
End If
Next checkRow
Next row
End Sub