More efficient way of deleting rows that do not meet criteria

aurelius89

Board Regular
Joined
Mar 15, 2017
Messages
69
I have some code that deletes rows that are not in a specified list of row numbers that are to be kept. It functions exactly as intended.

Code:
  For lRow = numRowsInBBS To 1 Step -1
    
    lMatch = 0
    On Error Resume Next
    lMatch = Application.Match(lRow, ws.Range("AE4:AE" & numRows).Value, 0&)
    On Error GoTo 0
    
    If Not CBool(lMatch) Then
      wsImport.Cells(lRow, 1).EntireRow.Delete
    End If
  Next
End Sub

However, this takes a monumental amount of time. To do this on 150 rows takes a couple of minutes of processing. I have documents that could be 1000s of rows long.

Is there a better way of achieving my goal here?

Essentially I want to delete all rows on a specified sheet EXCEPT for the row numbers specified in AE4:AE?? (This is calculated by numRows) on a different sheet.
 
Last edited:
Any success with this?
You have a lot going on in that workbook and I don't have the time right now to digest it all. One thing you might try though... if I understood what I saw correctly, it looks like Column AC (that column designation is from memory, but I think it is correct) contains the same numbers as Column AE but without all those 0's and with no gaps between the numbers. That is what the numbers looked like in the test sheet I made up originally, so try changing any references in my code from Column AE to Column AC and see if that makes it work for you.
 
Upvote 0

Excel Facts

Add Bullets to Range
Select range. Press Ctrl+1. On Number tab, choose Custom. Type Alt+7 then space then @ sign (using 7 on numeric keypad)
aurelius89..,

So you have a solution from elsewhere and what, you didn't plan to tell us?!!?!!??? Nice, real nice! {You do know what sarcasm is, don't you?}

NOW PLEASE READ THIS!!!!!

A message to forum cross posters
 
Last edited:
Upvote 0

Forum statistics

Threads
1,215,006
Messages
6,122,666
Members
449,091
Latest member
peppernaut

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