vba - 使用条件和删除整个重复行

标签 vba excel

我有重复的数据(“c”列),并且我想删除“D”列中包含数字的行。但仅适用于具有奇数日期的重复项,如图所示 enter image description here

这是我使用的代码,但我不知道如何删除“D”中数据的行并且是重复的

 Sub del_doops()
    Dim RowNdx As Long
    Dim RowNdx2 As Long

    For RowNdx = Range("A1:f1").End(xlDown).Row To 2 Step -1
        For RowNdx2 = RowNdx - 1 To 1 Step -1   'Begin at one above RowNdx

            If Cells(RowNdx, "b").Value = Cells(RowNdx2, "b").Value And _
                Cells(RowNdx, "C").Value = Cells(RowNdx2, "C").Value And _
                Cells(RowNdx, "E").Value = Cells(RowNdx2, "E").Value And _
                Cells(RowNdx, "F").Value <> Cells(RowNdx2, "F").Value Then
                Rows(RowNdx2).Delete 'this is where i need help
            End If

        Next RowNdx2
    Next RowNdx

End Sub

最佳答案

Sheet1 更改为 Set wS = ThisWorkbook.Sheets("Sheet1") 中工作表的名称:

Sub del_doops()
Dim RowNdx As Long
Dim RowNdx2 As Long
Dim wS As Worksheet

Set wS = ThisWorkbook.Sheets("Sheet1")
With wS
    For RowNdx = .Range("A" & .Rows.Count).End(xlUp).Row To 2 Step -1
        For RowNdx2 = RowNdx - 1 To 1 Step -1   'Begin at one above RowNdx
            If .Cells(RowNdx, "B").Value = .Cells(RowNdx2, "B").Value And _
                    .Cells(RowNdx, "C").Value = .Cells(RowNdx2, "C").Value And _
                    .Cells(RowNdx, "E").Value = .Cells(RowNdx2, "E").Value And _
                    .Cells(RowNdx, "F").Value <> .Cells(RowNdx2, "F").Value Then
                If .Cells(RowNdx, "D").Value <> vbNullString Then
                    .Rows(RowNdx).Delete
                Else
                    If .Cells(RowNdx2, "D").Value = vbNullString Then .Rows(RowNdx2).Delete
                End If
            End If
        Next RowNdx2
    Next RowNdx
End With 'wS
End Sub

关于vba - 使用条件和删除整个重复行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42530656/

相关文章:

c# - Entity Framework 、dll、excel

ms-access - 循环记录集并添加到字典/集合 : item key exists

excel - 如何使用 Excel VBA 将单元格值和文本连接在一起?

excel - 从表中的当前选择中删除重复项

excel - 如何避免 Power Query Formula.Firewall 权限错误?

python - 有没有一种方法可以将 Excel 工作簿合并到 python 中的一个主工作簿中?

excel - 如果不存在则创建文件夹路径(从 VBA 保存)

VBA - 如果 A 列中的单元格不是空白,则 B 列等于

vba - 在 VBA Access 中使用 .text 和 .value 之间的区别

vba - 如果列为空而不是第一个单元格,请执行此操作