2013-10-07 95 views
0

需要刪除Excel中CSV文件中的多行,這是基於表單1上列L與表單2上列A的匹配。內容是電子郵件地址。我怎樣才能做到這一點?根據匹配刪除整行?

因此,如果sheet2列A的電子郵件地址與sheet1 - 列L中的任何電子郵件地址相匹配,則應該從該電子郵件地址所在的sheet1中刪除整行。

工作表Sheet1如下: Sheet1

Sheet2的下面: Sheet2

@mehow,這裏的圖像,我得到當我運行代碼的模塊: Error with mehow's code

+0

嗯,你可以使用自動過濾器? –

+0

如何使用Autofilter? –

+0

你可以看到Autofilter Example [here](http://stackoverflow.com/questions/11631363/how-to-copy-a-line-in-excel-using-a-specific-word-and-pasting-to -otherother-excel -s) –

回答

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%。

+0

我把這個放在哪裏?是的,我在兩張表格中都有第一行的標題。 –

+0

將此作爲宏添加並運行,並刪除了sheet1中的所有內容,而sheet2保持不變。 –

+0

我收到錯誤:方法:對象範圍的自動篩選失敗。 –