VBA code to clean up a sheet (Entire Row Delete)

BallGazer

Board Regular
Joined
Jul 16, 2008
Messages
110
Hi Guys

I hope someone with more VBA than I have can help. I am trying to create code to clean up a sheet. What I need is to test the values in Column "T" starting at "T2". The cells in this column contains either a space or a delete flag (set at 1). If the flag is set to "1" I need to delete the entire row. The sheet contains ~ 300 rows. My attempts are falling foul of the relative positioning once I have deleted a row. I would appreciate any help with suitable code to run this cleanup.

Many thanks

BallGazer
 

Excel Facts

Links? Where??
If Excel says you have links but you can't find them, go to Formulas, Name Manager. Look for old links to dead workbooks & delete.
You need to work bottom up

Code:
With Activesheet

    LastRow = .Cells(.Rows.Count, "T").End(xlUp).Row
    For i = LastRow To 2 Step -1

        If .Cells(i, "T").Value = 1 Then

            .Rows(i).Delete
        End If
    Next i
End With
 
Upvote 0
Hi BallGazer ,

Why not use the autofilter? Filter column T2 so as to show the rows you want to delete, and then delete the visible rows?

If I needed to automate this process using VBA then this would also be the approach I would take.

HTH
 
Upvote 0
Try

Code:
Sub Cleanup()
Dim LR As Long, i As Long
Application.ScreenUpdating = False
LR = Range("T" & Rows.Count).End(xlUp).Row
For i = LR To 2 Step -1
    If Range("T" & i).Value = 1 Then Rows(i).Delete
Next i
Application.ScreenUpdating = True
End Sub
 
Upvote 0
Try this code:

Code:
Sub Test()
    Columns("T:T").AutoFilter
    Range("T1").Select
    Selection.AutoFilter Field:=1, Criteria1:="1"
    Range(Rows("2:2"), Range("T2").End(xlDown)).EntireRow.Delete Shift:=xlUp
    Selection.AutoFilter
End Sub
 
Upvote 0

Forum statistics

Threads
1,213,494
Messages
6,113,986
Members
448,538
Latest member
alex78

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