根据匹配删除整行?

时间:2013-10-07 14:22:43

标签: excel excel-vba multiple-columns excel-vba-mac vba

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

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

下面的Sheet1: Sheet1

下面的表2: Sheet2

@mehow,这是我将代码作为模块运行时得到的图像: Error with mehow's code

1 个答案:

答案 0 :(得分: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

注意: 如果您的数据有标题,请运行此代码,如果不提供建议。

记住 在您确信数据正常工作之前,请始终在数据副本上运行任何代码而不是实际数据。