Macro to delete all rows except .

Sunline

Well-known Member
Joined
Oct 6, 2007
Messages
701
Office Version
  1. 2016
Platform
  1. Windows
Hello all , i was wanting to get a basic macro that can delete all rows using col O except when Mdn is mentioned .
Also leave row 1 as heading , dont delete row 1 .
Thanks .
 
BTW, does your sheet have any formulas down those 170,000 rows and do you have any other code (eg Worksheet_Change code) in operation with the sheet?
In case the answer to either of these is 'Yes', you could try replacing the relevant section of my code with this.
Code:
If rws > 0 Then
    Application.ScreenUpdating = False
    Application.Calculation = xlCalculationManual
    Application.EnableEvents = False
    With Range("A2").Resize(lr - 1)
        .Offset(, lc).Value = tmp
        .Resize(, lc + 1).Sort Key1:=.Cells(1, lc + 1), _
            Order1:=xlAscending, Header:=xlNo
        .Resize(rws).EntireRow.Delete
        .Offset(, lc).ClearContents
    End With
    Application.EnableEvents = True
    Application.Calculation = xlCalculationAutomatic
    Application.ScreenUpdating = True
End If
 
Upvote 0

Excel Facts

Ambidextrous Undo
Undo last command with Ctrl+Z or Alt+Backspace. If you use the Undo icon in the QAT, open the drop-down arrow to undo up to 100 steps.
Hello Peter , sample sheet provided , im about to test macros against your latest test sheet .
Its a huge task to copy too another sheet as i have only mastered exporting values only in last couple of weeks . I do have macros which can run right thru the 171,000 rows , some can take up to 27 mins but thats ok .
The sheet does contain formulars but nothing to do with Col O . These functions are for a points system im developing for my own personal use .
Thanks .
Excel Workbook
ABCDEFGHIJKLMNOPQRSTUV
1IDPQYeaMonMeetDateDayWinPlaceConCon#WeatherRailTimeClassDistStakesR#FszRace NamePlacing
2225PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:00:00 p.m.MDN2100$30,000.00291
3226PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:00:00 p.m.MDN2100$30,000.00292
4227PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:00:00 p.m.MDN2100$30,000.00293
5228PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:00:00 p.m.MDN2100$30,000.00294
6229PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:00:00 p.m.MDN2100$30,000.00295
7230PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:00:00 p.m.MDN2100$30,000.00296
8231PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:00:00 p.m.MDN2100$30,000.00297
9232PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:00:00 p.m.MDN2100$30,000.00298
10233PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:00:00 p.m.MDN2100$30,000.00299
11242PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:35:00 p.m.3YO HCP1200$20,000.003101
12243PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:35:00 p.m.3YO HCP1200$20,000.003102
13244PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:35:00 p.m.3YO HCP1200$20,000.003103
14245PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:35:00 p.m.3YO HCP1200$20,000.003104
15246PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:35:00 p.m.3YO HCP1200$20,000.003105
16247PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:35:00 p.m.3YO HCP1200$20,000.003106
17248PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:35:00 p.m.3YO HCP1200$20,000.003107
18249PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:35:00 p.m.3YO HCP1200$20,000.003108
19250PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:35:00 p.m.3YO HCP1200$20,000.003109
20251PQ2006MarTren18/03/2006Saturday$0.00$0.00GoodFineTrue Position1:35:00 p.m.3YO HCP1200$20,000.0031010
Sheet1
 
Upvote 0
The sheet does contain formulars but nothing to do with Col O .
However, the formulas will recalculate during my code and that is likely to be slow with so many rows. Try replacing the section of code as described in post #21.

If that doesn't help significantly, tell us what any row 2 formulas are and what columns they are in.
 
Upvote 0
Peter i just got your original macro with your lastest amendment added to work .
It took just under 4 mins which is OK for me .

If i may ask one last request , as i didnt explain correctly from the start , this works great and i would of needed it to delete all but keep when word is mentioned anywhere in cell as there other classes simalar , which this macro does .
Can i get it to delete all except row 1 heading of course when cell equals MDN only . Not Mdn 3YO or Mdn C&G , keep Mdn , MDN , mDN , etc only .

I will keep working on this to get it down to the approx 3 second mark .
Thanks .
 
Upvote 0
Can i get it to delete all except row 1 heading of course when cell equals MDN only . Not Mdn 3YO or Mdn C&G , keep Mdn , MDN , mDN , etc only .
Try this. Change this line - note the UPPER CASE
Code:
Const LookForVal As String = "MDN"
and replace the first line below with the second one.
Code:
If InStr(1, aCol(i, 1), LookForVal, 1) = 0 Then
If UCase(aCol(i, 1)) <> LookForVal Then

Also, do you need the formulas to remain as formulas in the sheet or could they be replaced with their results?
 
Upvote 0
I made the changes you suggested in your last post and macro ran for 15 mins , when i stopped it it said ,
run time error - 2147417848(80010108) method sort of object range failed .

I would prefer to keep the formulars in the sheet , but i do take on board i may need too i think is it copy all as values .
So i will have to come to the best arrangement i can with the macro that works . Might have to do some sorting and deleting with smaller delete macros using <than , >than , between .
Thanks .
 
Upvote 0
I can't make the code take anything like that long so I suspect that your formulas may be the problem in terms of time.

Just to test, take a copy of your workbook, Copy and Paste Special Values all the formulas and run the code on it.
 
Upvote 0
Ok thanks for that i will make a copy and test . Will report back on this . Just at work and also realised i failed to tell you what functions are in my sheet , will advise .

Peter if i wanted to delete all except MDN , Rating 90 , 3YO F , 0-4Wins , how do i write this too include my last 3 suggestions along as MDN as well .

Just giving myself some more options with this .
Thanks .
 
Upvote 0
Peter if i wanted to delete all except MDN , Rating 90 , 3YO F , 0-4Wins , how do i write this too include my last 3 suggestions along as MDN as well .
Assuming they are all in column O I would change the code structure a little.

<font face=Courier New><br><SPAN style="color:#00007F">Sub</SPAN> Del_Rows()<br>    <SPAN style="color:#00007F">Dim</SPAN> lr <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, lc <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, i <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN>, rws <SPAN style="color:#00007F">As</SPAN> <SPAN style="color:#00007F">Long</SPAN><br>    <SPAN style="color:#00007F">Dim</SPAN> aCol, tmp<br>                     <br>    lr = Range("O" & Rows.Count).End(xlUp).Row<br>    lc = Cells(1, Columns.Count).End(xlToLeft).Column<br>    aCol = Range("O2:O" & lr).Value<br>    <SPAN style="color:#00007F">ReDim</SPAN> tmp(1 <SPAN style="color:#00007F">To</SPAN> lr - 1, 1 <SPAN style="color:#00007F">To</SPAN> 1)<br>    <SPAN style="color:#00007F">For</SPAN> i = 1 <SPAN style="color:#00007F">To</SPAN> lr - 1<br>        <SPAN style="color:#00007F">Select</SPAN> <SPAN style="color:#00007F">Case</SPAN> UCase(aCol(i, 1))<br>            <SPAN style="color:#00007F">Case</SPAN> "MDN", "RATING 90", "3YO F", "0-4WINS"<br>               <br>            Case <SPAN style="color:#00007F">Else</SPAN><br>                rws = rws + 1<br>                tmp(i, 1) = 1<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Select</SPAN><br>    <SPAN style="color:#00007F">Next</SPAN> i<br>    <SPAN style="color:#00007F">If</SPAN> rws > 0 <SPAN style="color:#00007F">Then</SPAN><br>        Application.ScreenUpdating = <SPAN style="color:#00007F">False</SPAN><br>        Application.Calculation = xlCalculationManual<br>        Application.EnableEvents = <SPAN style="color:#00007F">False</SPAN><br>        <SPAN style="color:#00007F">With</SPAN> Range("A2").Resize(lr - 1)<br>            .Offset(, lc).Value = tmp<br>            .Resize(, lc + 1).Sort Key1:=.Cells(1, lc + 1), _<br>                Order1:=xlAscending, Header:=xlNo<br>            .Resize(rws).EntireRow.Delete<br>            .Offset(, lc).ClearContents<br>        <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">With</SPAN><br>        Application.EnableEvents = <SPAN style="color:#00007F">True</SPAN><br>        Application.Calculation = xlCalculationAutomatic<br>        Application.ScreenUpdating = <SPAN style="color:#00007F">True</SPAN><br>    <SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">If</SPAN><br>    MsgBox "Done"<br><SPAN style="color:#00007F">End</SPAN> <SPAN style="color:#00007F">Sub</SPAN><br></FONT>
 
Upvote 0
Thanks for this Peter will try out over the weekend .
Thanks too all for your posts .
 
Upvote 0

Forum statistics

Threads
1,224,527
Messages
6,179,348
Members
452,907
Latest member
Roland Deschain

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