![](/img/trans.png)
[英]How to delete rows from excel files if a cell in a particular column contains the string of array?
[英]Excel- Delete rows if a cell in a particular column contains values found in another column
如果要在另一列中找到“供應商”列中的值,我想刪除所有行。 (lookupVendor Col在另一張紙上)
MainSheet
LookupColumn
VendorCol
google
microsoft
apple
vendorA
VendorB
這是我所擁有的,但只刪除一個值。 如何使用整個LookupColumn並檢測在lookupColumn中找到colc值的任何行。 我的簡單VBA代碼一次只能處理一個值(效率不高)
Sub Find_Vendor()
Dim rng As Range
Dim what As String
what = "Microsoft"
Do
Set rng = ActiveSheet.UsedRange.Find(what)
If rng Is Nothing Then
Exit Do
Else
Rows(rng.Row).Delete
End If
Loop
End Sub
Mainsheet:包含600,000行LookupColumn包含400個條目。 對於我擁有的數據量,現有代碼非常慢。
使用AutoFilter():
Option Explicit
Public Sub Find_Vendor()
Dim filters As Variant
With Sheets("LookupColumnSheet") ' change "LookupColumnSheet" to your actual sheet with lookup values name
filters = Application.Transpose(.Range("A1", .Cells(.Rows.Count, 1).End(xlUp)).Value) ' collect "LookupColumnSheet" sheet column A values from row 1 down to last not empty row
End With
With Sheets("MainSheet") 'reference "MainSheet" sheet (change "MainSheet" to your actual "MainSheet" sheet name
With .Range("C1", .Cells(.Rows.Count, 3).End(xlUp)) ' reference referenced sheet column C cells from row 1 down to last not empty one
.AutoFilter Field:=1, Criteria1:=filters, Operator:=xlFilterValues ' filter referenced range with values from "LookupColumnSheet" sheet column A
If Application.WorksheetFunction.Subtotal(103, .Cells) > 1 Then .Resize(.Rows.Count - 1).Offset(1).SpecialCells(xlCellTypeVisible).EntireRow.Delete ' if any filtered cells other than header then delete their entire row
End With
.AutoFilterMode = False ' remove filters
End With
End Sub
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.