Delete Rows NOT based on a criteria

chiswickbridge

Board Regular
Joined
Feb 2, 2013
Messages
130
In Sheet1, I have a list of 100 Dynamic IDs in Column D. Starting from D11 to D111

In Sheet2, I have a database of about 20,000 rows. Each ID ( from Sheet1 ) will appear only once in Sheet2.ColA.

Need a VBA to delete rows, where the value in Column A is NOT equal to any of the 100 ID.

In short, I need a procedure to trim the 20,000 rows down to 100 rows only.

Also, if possible to sort the Sheet2 data, in the same order Sheet1
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hi chiswickbridge,

Try this (though initially on a copy of your data as the results cannot be undone if they're not as expected):

Code:
Option Explicit
Sub Macro1()
    
    Dim lngLastRow As Long
    Dim lngMyCol As Long
    Dim strMyCol As String
    Dim blnRowsDeleted As Boolean
    
    Application.ScreenUpdating = False
    
    lngLastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    lngMyCol = Cells.Find("*", SearchOrder:=xlByColumns, SearchDirection:=xlPrevious).Column + 1
    strMyCol = Split(Cells(1, lngMyCol).Address, "$")(1)
    
    With Sheets("Sheet2").Range(strMyCol & "2:" & strMyCol & lngLastRow) 'Starts from Row 2 in Sheet2. Change to suit.
        .Formula = "=IFERROR(VLOOKUP(A2,Sheet1!D:D,1,FALSE),""DEL"")"
        .Value = .Value
        .Replace "DEL", "#N/A", xlWhole, , False, , False, False
        On Error Resume Next 'Ignore 'No cells were found' message
            .SpecialCells(xlConstants, xlErrors).EntireRow.Delete
            If Err.Number <> 0 Then
                blnRowsDeleted = False
            Else
                blnRowsDeleted = True
            End If
        On Error GoTo 0
    End With
    
    Sheets("Sheet2").Columns(strMyCol).EntireColumn.Delete
    
    Application.ScreenUpdating = True
    
    If blnRowsDeleted = True Then
        MsgBox "Non matching rows have now been deleted.", vbInformation
    Else
        MsgBox "There were no rows identified to be deleted.", vbExclamation
    End If
        
End Sub

Regards,

Robert
 
Upvote 0

Forum statistics

Threads
1,223,898
Messages
6,175,274
Members
452,628
Latest member
dd2

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top