Did you mean column O = "Mdn" or column O contains "Mdn" somewhere?delete all rows using col O except when Mdn is mentioned .
Sub deletrow()
Dim lr&, lc&, u()
Dim rgo, i&, k&
lr = Cells.Find("*", after:=Cells(1), searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
lc = Cells.Find("*", after:=Cells(1), searchorder:=xlByColumns, _
searchdirection:=xlPrevious).Column
ReDim u(1 To lr, 1 To 1)
rgo = Range("O1").Resize(lr)
For i = 2 To lr
If InStr(rgo(i, 1), "Mdn") > 0 Then u(i, 1) = 1: k = k + 1
Next i
Cells(1, lc + 1).Resize(lr) = u
Range("A1", Cells(lr, lc + 1)).Sort Cells(1, lc + 1), 1, Header:=xlYes
Range("A" & k + 2, Cells(lr, lc)).ClearContents
Cells(1, lc + 1).Resize(lr).ClearContents
MsgBox "Done"
End Sub
Hi Peter,mirabeau
You most likely recognise the code in my post as a very slightly modified version of code you have posted previously (that I was clearly impressed by since I have reproduced it a few times since ).
Your code here is a little faster again. Would that basically be put down to not having to insert/delete column A? Perhaps also ClearContents as opposed to deleting rows?
Sub deletrow2() 'based on juggling arrays
t = Timer
Dim lr&, lc&, a, c()
Dim i&, j&, k&
lr = Cells.Find("*", after:=Cells(1), searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
lc = Cells.Find("*", after:=Cells(1), searchorder:=xlByColumns, _
searchdirection:=xlPrevious).Column
a = Range("A1").Resize(lr, lc)
ReDim c(1 To lr, 1 To lc)
For i = 2 To lr
If InStr(a(i, 15), "Mdn") > 0 Then
k = k + 1
For j = 1 To lc
c(k, j) = a(i, j)
Next j
End If
Next i
Range("A2").Resize(lr - 1, lc).ClearContents
Range("A2").Resize(k, lc) = c
MsgBox Format(Timer - t, "0.000")
End Sub
Sub delrow() 'for Excel 2007 et seq, based on Remove Duplicates
t = Timer
Dim lr&, lc&, u()
Dim rgo, i&, k&
lr = Cells.Find("*", after:=Cells(1), searchorder:=xlByRows, _
searchdirection:=xlPrevious).Row
lc = Cells.Find("*", after:=Cells(1), searchorder:=xlByColumns, _
searchdirection:=xlPrevious).Column
ReDim u(1 To lr, 1 To 1)
rgo = Range("O1").Resize(lr)
For i = 2 To lr
If InStr(rgo(i, 1), "Mdn") > 0 Then u(i, 1) = i
Next i
Cells(1, lc + 1).Resize(lr) = u
Range("A1").Resize(lr, lc + 1).RemoveDuplicates Columns:=lc + 1, Header:=xlYes
Cells(2, lc + 1).Resize(lr - 1).SpecialCells(4).EntireRow.Delete 'to remove the sole remaining blank
Cells(1, lc + 1).Resize(lr).ClearContents
MsgBox Format(Timer - t, "0.000")
End Sub
My code keeps the row if "Mdn" is contained somewhere in the cell and is not case sensitive.
This just means that my code will keep the row if column O contains mdn whether it is upper case, lower case or a mixture, so all thes (and more) would be kept.im not sure what case sensitive is in sorry .
Something definitely appears to be going wrong. We obviously do not have your actual data but I agree with mirabeau that our macros have been completing in well under 3 seconds. I have actually been testing with about 200,000 rows and data in columns A:O, with "Mdn" appearing somewhere in about 60,000 of those rows.Peter yours ran for 15 mins so i thought this also may not be working , i then went ctrl-alt-delete , will let it run when im sleeping as it may take awhile .
Excel Workbook | |||||||||||||||||
---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
A | B | C | D | E | F | G | H | I | J | K | L | M | N | O | |||
1 | H1 | H2 | H3 | H4 | H5 | H6 | H7 | H8 | H9 | H10 | H11 | H12 | H13 | H14 | H15 | ||
2 | fg | yu | ui | i | ytu | fsdf | fsd | x | x | x | rtu | o | x | g | fghfg | ||
3 | s | k | l | yui | yu | op | x | x | o | i | iup | o[' | h | aaMdn | |||
4 | g dthg | uk | gf | jh | k | l; | l | x | x | d | i | dfsd | p] | bh | yrtyu | ||
5 | h | ui | hg | k | hl | jk | as | hg | c | z | d | hj | x | f | |||
6 | trh | l | jg | h | [] | ad | gfd | j | fxdg | d | s | gh | x | x | Mdn | ||
7 | tyj | iuol | po' | u | 9= | yt | x | x | x | x | x | x | x | x | fghfg | ||
8 | x | x | x | x | x | x | x | x | x | x | x | x | x | x | aaMdn | ||
Del Rows |