Hi I have the following code below which I like because it asks the user for the column they want to search on and what to search for..Problem is there are like 200 Thousand rows and that code seems to just hang..Is there a more efficient way to delete rows lets say that contain the number 1 in column B and delete it if does and of course shift rows up if it does..
Code:
[COLOR=blue]Option Explicit[/COLOR]
[COLOR=blue]Sub[/COLOR] KillRows()
[COLOR=blue]Dim[/COLOR] MyRange [COLOR=blue]As[/COLOR] Range, DelRange [COLOR=blue]As[/COLOR] Range, C [COLOR=blue]As[/COLOR] Range
[COLOR=blue]Dim[/COLOR] MatchString [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR], SearchColumn [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR], ActiveColumn [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
[COLOR=blue]Dim[/COLOR] FirstAddress [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR], NullCheck [COLOR=blue]As[/COLOR] [COLOR=blue]String[/COLOR]
[COLOR=blue]Dim[/COLOR] AC
[COLOR=darkgreen]'Extract active column as text[/COLOR]
AC = Split(ActiveCell.EntireColumn.Address(, [COLOR=blue]False[/COLOR]), ":")
ActiveColumn = AC(0)
SearchColumn = InputBox("Enter Search Column - press Cancel to exit sub", "Row Delete Code", ActiveColumn)
[COLOR=blue]On Error Resume Next[/COLOR]
[COLOR=blue]Set[/COLOR] MyRange = Columns(SearchColumn)
[COLOR=blue]On Error Goto[/COLOR] 0
[COLOR=darkgreen]'If an invalid range is entered then exit[/COLOR]
[COLOR=blue]If[/COLOR] MyRange [COLOR=blue]Is[/COLOR] [COLOR=blue]Nothing[/COLOR] [COLOR=blue]Then[/COLOR] Exit [COLOR=blue]Sub[/COLOR]
MatchString = InputBox("Enter Search string", "Row Delete Code", ActiveCell.Value)
[COLOR=blue]If[/COLOR] MatchString = "" [COLOR=blue]Then[/COLOR]
NullCheck = InputBox("Do you really want to delete rows with empty cells?" & vbNewLine & vbNewLine & _
"Type Yes to do so, else code will exit", "Caution", "No")
[COLOR=blue]If[/COLOR] NullCheck <> "Yes" [COLOR=blue]Then[/COLOR] Exit [COLOR=blue]Sub[/COLOR]
[COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
Application.ScreenUpdating = [COLOR=blue]False[/COLOR]
[COLOR=darkgreen]'to match the WHOLE text string[/COLOR]
[COLOR=blue]Set[/COLOR] C = MyRange.Find(What:=MatchString, After:=MyRange.Cells(1), LookIn:=xlValues, Lookat:=xlWhole)
[COLOR=darkgreen]'to match a PARTIAL text string use this line[/COLOR]
[COLOR=darkgreen]'Set C = MyRange.Find(What:=MatchString, After:=MyRange.Cells(1), LookIn:=xlValues, Lookat:=xlpart)[/COLOR]
[COLOR=darkgreen]'to match the case and of a WHOLE text string[/COLOR]
[COLOR=darkgreen]'Set C = MyRange.Find(What:=MatchString, After:=MyRange.Cells(1), LookIn:=xlValues, Lookat:=xlWhole, MatchCase:=True)[/COLOR]
[COLOR=blue]If[/COLOR] [COLOR=blue]Not[/COLOR] C [COLOR=blue]Is[/COLOR] [COLOR=blue]Nothing[/COLOR] [COLOR=blue]Then[/COLOR]
[COLOR=blue]Set[/COLOR] DelRange = C
FirstAddress = C.Address
[COLOR=blue]Do[/COLOR]
[COLOR=blue]Set[/COLOR] C = MyRange.FindNext(C)
[COLOR=blue]Set[/COLOR] DelRange = Union(DelRange, C)
[COLOR=blue]Loop[/COLOR] [COLOR=blue]While[/COLOR] FirstAddress <> C.Address
[COLOR=blue]End[/COLOR] [COLOR=blue]If[/COLOR]
[COLOR=darkgreen]'If there are valid matches then delete the rows[/COLOR]
[COLOR=blue]If[/COLOR] [COLOR=blue]Not[/COLOR] DelRange [COLOR=blue]Is[/COLOR] [COLOR=blue]Nothing[/COLOR] [COLOR=blue]Then[/COLOR] DelRange.EntireRow.Delete
Application.ScreenUpdating = [COLOR=blue]True[/COLOR]
[COLOR=blue]End Sub[/COLOR]