0

需要根据 sheet1 上的 L 列与 sheet2 上的 A 列的匹配,在 Excel 中删除 CSV 文件中的多行。内容是电子邮件地址。我怎样才能做到这一点?

因此,如果 sheet2 列 A 的电子邮件地址与 sheet1 - 列 L 中的任何电子邮件地址匹配,则它应该从该电子邮件地址所在的 sheet1 中删除整行。

下面的表 1: 表 1

下面的表 2: 表 2

@mehow,这是我将代码作为模块运行时得到的图像: mehow的代码错误

4

1 回答 1

1

这对于您想要做的事情非常快,因为它不涉及任何循环。

Sub DeleteDuplicates()

Dim StartingScreenUpdateValue As Boolean
Dim StartingEventsValue As Boolean
Dim StartingCalculations As XlCalculation

With Application
    StartingScreenUpdateValue = .ScreenUpdating
    StartingEventsValue = .EnableEvents
    StartingCalculations = .Calculation
    .ScreenUpdating = False
    .EnableEvents = False
    .Calculation = xlCalculationManual
End With


Dim varTestValues As Variant
Dim sh1 As Worksheet
Dim sh2 As Worksheet

Set sh1 = Sheets("Sheet1")
Set sh2 = Sheets("Sheet2")

With sh2
    varTestValues = .Range("A1", .Range("A" & .Rows.Count).End(xlUp))
End With

sh1.Range("L1", sh1.Range("L" & sh1.Rows.Count).End(xlUp)) _
    .AutoFilter Field:=12, Criteria1:=Application.Transpose(varTestValues), Operator:=xlFilterValues

sh1.Range("L2", sh1.Range("L" & sh1.Rows.Count).End(xlUp)) _
    .SpecialCells(xlCellTypeVisible).EntireRow.Delete

sh1.AutoFilterMode = False

With Application
    .ScreenUpdating = StartingScreenUpdateValue
    .EnableEvents = StartingEventsValue
    .Calculation = StartingCalculations
End With

End Sub

注意:此代码运行假设您的数据有标题,如果没有请告知。

请记住,始终在您的数据副本上运行任何代码,而不是在您的实际数据上运行,直到您确信它可以 100% 运行。

于 2013-10-07T15:02:52.157 回答