Deleting rows based on 2 criteria

MichaelaM

New Member
Joined
Apr 2, 2018
Messages
9
Hi,

I am totally new with VBA and was trying to make a macro deleting rows based on certain criteria. With one criteria it worked just fine, but I am struggling to add a second criteria. In short, below the code that I have so far. I need it to delete all rows that do not contain "Sales" in column C and "Bonus" in column G. Could anyone help?

Application.ScreenUpdating = False
With Range("C3", Range("C" & Rows.Count).End(xlUp))
.AutoFilter Field:=1, Criteria1:="<>*Sales*"
.Offset(1).EntireRow.Delete
.AutoFilter
End With
Application.ScreenUpdating = True
 

Excel Facts

Format cells as currency
Select range and press Ctrl+Shift+4 to format cells as currency. (Shift 4 is the $ sign).
Hi & welcome to the board.
How about
Code:
With Range("C3", Range("G" & Rows.Count).End(xlUp))
   .AutoFilter Field:=1, Criteria1:="<>*Sales*"
   .AutoFilter 5, "Bonus"
   .Offset(1).EntireRow.Delete
   .AutoFilter
End With
 
Upvote 0
Hi there :)

Thanks a lot for the quick replies, but unfortunately neither worked out. I have a report with 30 columns and over 60k rows. I need to filter and delete all rows, that do not contain "Sales" in column C and "Bonus" in Column G. Rows 1-2 contain explanations, row 3 the header and basically it should start filtering and deleting from row 4.

Thanks a lot once again!
 
Upvote 0
In what way isn't it working?
 
Upvote 0
Hi there. I modified your code very slightly, but I think this should work.

Application.ScreenUpdating = False


lRow = Range("C3").End(xlDown).Row


Range("C3:G" & lRow).AutoFilter Field:=1, Criteria1:="Sales"
Range("C3:G" & lRow).AutoFilter Field:=5, Criteria1:="Bonus"




Application.ScreenUpdating = True


End Sub
 
Upvote 0
Do you mean:
all rows, that do not contain "Sales" in column C and
do not contain "Bonus" in column G
 
Upvote 0
Hi Fluff,

After running the Macro it returned only 2 rows, whereas they should be around 3000 and something.

Hi My Aswer Is This Yes, all columns that do not contain "Sales" in column C and do not contain "Bonus" in column G need to be deleted (not just filtered).
 
Upvote 0
In that case try
Code:
   Dim Usdrws As Long
   
   Usdrws = Cells.find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
   Application.ScreenUpdating = False
   With Range("C3:G" & Usdrws)
      .AutoFilter 1, "<>*Sales*"
      .AutoFilter 5, "<>*Bonus*"
      .Offset(1).EntireRow.Delete
      .AutoFilter
   End With
   Application.ScreenUpdating = True
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,181
Members
453,022
Latest member
Mohamed Magdi Tawfiq Emam

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