简体   繁体   中英

Excel- Delete rows if a cell in a particular column contains values found in another column

I want to delete all the rows if the value in Vendor column is found in another column. (lookupVendor Col in another sheet)

在此处输入图片说明

MainSheet

LookupColumn

VendorCol
google 
microsoft
apple
vendorA
VendorB

Here is what I have but it only delete for one value. How can I make it use the entire LookupColumn and detete any rows that colc value is found in lookupColumn. My simple VBA code handles one value at a time.(which is not efficient)

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: Contains 600,000 rows LookupColumn contains 400 entries. The existing code is super slow for the volume of data I have.

use 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM