deleting with a for loop

Flyingmeatball

Board Regular
Joined
Aug 15, 2007
Messages
65
I have a for loop that goes through cells in Column A, and deletes them if they are 0 or blank. However, if i delete a cell, the next row is in effect skipped, because the cells shift up. How can I make the for loop run twice if it deleted a cell? I tried deleteloop = deleteloop -1, but that just hangs my excel and i have to break the code. Here is the code:

Sub clearblanks()

Dim lastopen As Integer
With Sheets("upload file")
lastopen = .Cells(.Rows.Count, "A").End(xlUp).Row - 19
End With

For deleteloop = 20 To 20 + lastopen

If Cells(deleteloop, 4).Text = "" Or Cells(deleteloop, 4).Value = 0 Then
Rows(deleteloop).Delete
End If

Next deleteloop

End Sub
 

Excel Facts

How to total the visible cells?
From the first blank cell below a filtered data set, press Alt+=. Instead of SUM, you will get SUBTOTAL(9,)
Hello,

no, just the loop part.

Sub clearblanks()

Code:
Dim lastopen As Integer
With Sheets("upload file")
lastopen = .Cells(.Rows.Count, "A").End(xlUp).Row - 19
End With

For deleteloop = 20 + lastopen to 20 step -1

If Cells(deleteloop, 4).Text = "" Or Cells(deleteloop, 4).Value = 0 Then
Rows(deleteloop).Delete
End If

Next deleteloop

End Sub
 
Upvote 0
This was helpful for me..I am so used to just looking at a spreadsheet from top to bottom that the idea of running a loop from the bottom up just never entered my mind..Saving this one for future.

Shaun
 
Upvote 0
Never even knew that was possible. Does that mean I have to write the code in reverse too?
Yes, you have to set your international setting to a language like Arabic that runs right-to-left and then type the code in backwards. ;)

A couple of quick comments:
  • Generally, you want to avoid dimensioning variables that will handle row position as integers; long is more robust.
  • Looks like you could use autofilter here which will usually outperform a loop.
 
Upvote 0
Why avoid integers? I will never have a row position that is anything but an integer.

I'd love to hear how to use autofilter - not familiar with it at all. My visual basic knowledge is limited to what I've used and what I can parse over from my basic java skills :)
 
Upvote 0
Why avoid integers? I will never have a row position that is anything but an integer.
Emma covered this.

I'd love to hear how to use autofilter - not familiar with it at all. My visual basic knowledge is limited to what I've used and what I can parse over from my basic java skills
Try doing this by hand using Autofilter and the macro recorder. Then post back if you can't suss out how to apply what you see. I've seen enough of Audrey & Drafter's work to be quite certain that they can help you create an autofilter-using VBA solution, no prob.
 
Upvote 0

Forum statistics

Threads
1,216,090
Messages
6,128,765
Members
449,467
Latest member
sdafasfasdf

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