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

Does the VLOOKUP table have to be sorted?
No! when you are using an exact match, the VLOOKUP table can be in any order. Best-selling items at the top is actually the best.

xld

Banned
Joined
Feb 8, 2003
Messages
5,378
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
 

Colin Legg

MrExcel MVP
Joined
Feb 28, 2008
Messages
3,497
Office Version
  1. 365
  2. 2016
Platform
  1. Windows
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
 

VoG

Legend
Joined
Jun 19, 2002
Messages
63,650
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
 

pavin

Active Member
Joined
Jan 20, 2009
Messages
308
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
 

Forum statistics

Threads
1,144,628
Messages
5,725,378
Members
422,622
Latest member
Paranthem

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
Top