Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations strongm on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

HELP...For Next...Loop is not working right 1

Status
Not open for further replies.

Nick34

Technical User
Oct 16, 2003
50
US
Is there any reason why this code would not delete rows that have empty cells in the 3rd column after being run only once???

It must be run about 6 or 7 times before all of the rows with empty cells are deleted.

Sub RemoveBlanks()

Dim oCell As Object

Worksheets("SO2REMOVAL1").Select

Application.Goto "R2C3"

Range(Selection, Selection.End(xlDown)).Select

For Each oCell In Selection

If oCell = "" Then
'Debug.Print oCell.Offset(0, -1).Value
Rows(RowIndex:=(oCell.Row)).Delete
End If

Next oCell

End Sub
 
Because you are deleting from "front to back". After you delete the current row, the next row becomes the current row and the "Next" promptly "leapfrogs" it. I perform "deletes" from collections from "back to front"

Can you do
Code:
For i = Selection.Count - 1 to 0 Step -1
    oCell = Selection(i)    
    If oCell = "" Then
      'Debug.Print oCell.Offset(0, -1).Value
       Rows(RowIndex:=(oCell.Row)).Delete
   End If
Next


Forms/Controls Resizing/Tabbing Control
Compare Code (Text)
Generate Sort Class in VB or VBScript
 
Thank you. I had no idea you could work backward. Could you just explain why you subtracted 1 from the count and went to 0? Why not just go from the count to 1?
 
How about:
1) sort by column 3
2) use the xlUp/xlDown trick to determine the first empty row
3) delete a range of rows in one step
 

Thanks to Tony Jollans for this one - to delete all blanks in column c:

Columns("C:C").SpecialCells(xlCellTypeBlanks).EntireRow.Delete

 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top