VBA delete rows

Tom F

Active Member
Joined
Oct 19, 2002
Messages
263
What VBA code will delete rows in Worksheet 1 if the value of 6 appears in column W going from the last non-blank cell up to row 2?

Many thanks for your help,
Tom
 

Excel Facts

How can you turn a range sideways?
Copy the range. Select a blank cell. Right-click, Paste Special, then choose Transpose.
Try

Code:
Sub delw6()
Dim LastRow As Long, i As Long
LastRow = Sheets("Sheet1").Cells(Rows.Count, 23).End(xlUp).Row
Application.Calculation = xlManual
Application.ScreenUpdating = False
For i = LastRow To 2 Step -1
    With Sheets("Sheet1")
        If .Cells(i, 23).Value = 6 Then .Rows(i).EntireRow.Delete
    End With
Next i
Application.Calculation = xlAutomatic
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Hi Tom

Depending on the number of rows that are likely to require deletion, then Autofilter has the potential to be significantly faster than looping:

Code:
Sub Del_6()
With Columns("W")
    .AutoFilter field:=1, Criteria1:=6
    .Resize(Rows.Count - 1).Offset(1).EntireRow.Delete
End With
ActiveSheet.AutoFilterMode = False
End Sub
 
Upvote 0

Forum statistics

Threads
1,215,519
Messages
6,125,294
Members
449,218
Latest member
Excel Master

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