Quickly delete rows in a range containing certain value

bran

New Member
Joined
Jun 14, 2011
Messages
9
Hi, this is my first post so please let me know any mistakes.

I have a worksheet of 10,000 rows containing data seperated into multiple columns. I want to select all the cells in one column that don't contain a user input value and then delete all the rows containing those cells.

I can do this with a loop but it takes far too long. I'm really looking for any method that can quickly get rid of the unwanted rows that doesn't use loops.
 

Excel Facts

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
Try like this

Code:
Sub Del_x()
Dim x
x = InputBox("Enter criterion")
With Columns("W")
    .AutoFilter field:=1, Criteria1:=x
    .Resize(Rows.Count - 1).Offset(1).EntireRow.Delete
End With
ActiveSheet.AutoFilterMode = False
End Sub
 
Upvote 0
Thanks, I have the right idea now.

However, I wanted to delete every row without the input value. So if I had entered "prog" as the value, everything row not containing "prog" in a the column would be deleted.
 
Upvote 0
So change Vog's code to:
Code:
Sub Del_x()
Dim x
x = InputBox("Enter criterion")
With Columns("W")
    .AutoFilter field:=1, Criteria1:[COLOR="Red"][B]<>[/B][/COLOR]x
    .Resize(Rows.Count - 1).Offset(1).EntireRow.Delete
End With
ActiveSheet.AutoFilterMode = False
End Sub
[/code[
 
Upvote 0
I love this code as it gives me the option to input any data/word to delete a row. It's brililant.
However, being new to VBA/Macros etc, and being stupid and asking a stupid question, how do you change the code so it will look at all the columns as opposed to just the one in this code?
Also, is it easy to put code in to do a repeat/loop so that it prompts again for another word (I have various "criteria" words I search for in my documents) and when finished with typing the criteria it stops then runs the delete ro function?

Regards
Paul
 
Upvote 0
Try like this

Code:
Sub Del_x()
Dim x
x = InputBox("Enter criterion")
With Columns("W")
    .AutoFilter field:=1, Criteria1:=x
    .Resize(Rows.Count - 1).Offset(1).EntireRow.Delete
End With
ActiveSheet.AutoFilterMode = False
End Sub

===============

Vog sir,

I read this query, i tryed this, i got input msg box also.
But i didnt get in which scenario this will use?

If u have time, then pls answer...or require ur kind suggestion.
 
Upvote 0
It deletes rows where the value in column W is the same as the value that you enter in the input box.
 
Upvote 0

Forum statistics

Threads
1,224,521
Messages
6,179,285
Members
452,902
Latest member
Knuddeluff

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