Best way to write this??

spcalan

Well-known Member
Joined
Jun 4, 2008
Messages
1,247
I am trying to delete the entire row if the column J is greater than 1.
I know there are million ways to writethis.

Any suggestions?


Sub delete_columnj()
x = 1
Do Until x > 10000
If Sheets("sheet1").Cells(x, 10) > 1 Then
Rows(x).Select
Selection.Delete Shift:=xlUp
Else
x = x + 1
End If

Loop

End Sub
 

Excel Facts

How to fill five years of quarters?
Type 1Q-2023 in a cell. Grab the fill handle and drag down or right. After 4Q-2023, Excel will jump to 1Q-2024. Dash can be any character.

Jonmo1

MrExcel MVP
Joined
Oct 12, 2006
Messages
44,061
3 improvements...

1. Don't use select, that's very slow
2. Go backwards, from the bottom up..
3. Turn off ScreenUpdating and Events to speed things up..

Code:
Sub delete_columnj()
 
'Determine the last used row in column J
LR = Sheets("sheet1").Cells(Rows.Count,"J").End(xlup).row
 
'turn off screenupdating and events (greatly improve speed)
Application.EnableEvents = False
Application.Screenupdating = False
 
'Loop from the bottom (LR) to 1 going backwards..
For x = LR to 1 Step - 1
    If Sheets("sheet1").Cells(x,"J") > 1 Then
        Sheets("sheet1").Rows(x).EntireRow.Delete
    End If
Next x
 
'turn Back On screenupdating and events
Application.EnableEvents = True
Application.Screenupdating = True
 
End Sub

Hope this helps..
 
Upvote 0

spcalan

Well-known Member
Joined
Jun 4, 2008
Messages
1,247
3 improvements...

1. Don't use select, that's very slow
2. Go backwards, from the bottom up..
3. Turn off ScreenUpdating and Events to speed things up..

Code:
Sub delete_columnj()
 
'Determine the last used row in column J
LR = Sheets("sheet1").Cells(Rows.Count,"J").End(xlup).row
 
'turn off screenupdating and events (greatly improve speed)
Application.EnableEvents = False
Application.Screenupdating = False
 
'Loop from the bottom (LR) to 1 going backwards..
For x = LR to 1 Step - 1
    If Sheets("sheet1").Cells(x,"J") > 1 Then
        Sheets("sheet1").Rows(x).EntireRow.Delete
    End If
Next x
 
'turn Back On screenupdating and events
Application.EnableEvents = True
Application.Screenupdating = True
 
End Sub

Hope this helps..

Thank you for the help, and expecially the quotes on what exactly each line means.

Best so far.
 
Upvote 0

Forum statistics

Threads
1,191,166
Messages
5,985,051
Members
439,935
Latest member
Monty238

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