Advice on delete row VBA code

VBA learner ITG

Active Member
Joined
Apr 18, 2017
Messages
272
Office Version
  1. 365
Platform
  1. Windows
  2. MacOS
Hi Peers,

Can i get your advice to see if there is a quicker way of deleting rows than the piece of code that i wrote.




Sheets("sheet1").Select
Rows("1:1").Select
Selection.AutoFilter
ActiveSheet.Range("$1:$158").AutoFilter Field:=4, Criteria1:="=MIXED SEL 2" _
, Operator:=xlOr, Criteria2:="=MIXED SEL 4"
ActiveSheet.Range("$1:$158").AutoFilter Field:=3, Criteria1:="B"
Rows("13:13").Select
Range(Selection, Selection.End(xlDown)).Select
Range(Selection, Selection.End(xlDown)).Select
Selection.DELETE Shift:=xlUp
Range("I154").Select
ActiveSheet.Range("$1:$150").AutoFilter Field:=4
ActiveWindow.SmallScroll Down:=45
Range("D73").Select
ActiveWindow.SmallScroll Down:=-57
 

Excel Facts

Excel Joke
Why can't spreadsheets drive cars? They crash too often!
It might be easier to help if you could provide a detailed explanation in words of what you want to do. It would also help if you could attach a screen shot of what your data looks like.
 
Upvote 0
Hi Mumps,

Thank you for coming back to me:

On Sheet1
Criteria 1 = Column D if contains text "MIXED SEL 2" OR / AND "MIXED SEL 4"
Criteria 2 = Column C if contains text "B"
Delete all rows based on the above criterias.
 
Upvote 0
If column C contains "B", does column D also have to contain "MIXED SEL 2" OR / AND "MIXED SEL 4" in the same row as the "B" in order for the row to be deleted?
 
Upvote 0
Hi Mumps,

Thank you for coming back to me: Yes thats correct

"Column C contains "B", does column D also have to contain "MIXED SEL 2" OR / AND "MIXED SEL 4" in the same row as the "B"
 
Upvote 0
The macro assumes that you have headers in row 1 and that your data starts in row 2.
Code:
Sub DeleteRows()
    Application.ScreenUpdating = False
    Dim LastRow As Long
    LastRow = Cells.Find("*", SearchOrder:=xlByRows, SearchDirection:=xlPrevious).Row
    Range("C1:D" & LastRow).AutoFilter Field:=2, Criteria1:="=MIXED SEL 2", Operator:=xlOr, Criteria2:="=MIXED SEL 4"
    Range("C1:D" & LastRow).AutoFilter Field:=1, Criteria1:="B"
    Range("C2:D" & LastRow).SpecialCells(xlCellTypeVisible).EntireRow.Delete
    If ActiveSheet.AutoFilterMode = True Then ActiveSheet.AutoFilterMode = False
    Application.ScreenUpdating = True
End Sub
 
Upvote 0
Thank you Mumps for you help on tidying up my code and providing a new logical approach to trackling this issue in the future.

Your code ran much faster than my written code.

Thank you
 
Upvote 0

Forum statistics

Threads
1,224,823
Messages
6,181,173
Members
453,021
Latest member
Justyna P

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