Consulting

Results 1 to 4 of 4

Thread: Deleting rows based on criteria

  1. #1

    Deleting rows based on criteria

    HI There

    I have the folowing code that search for non numeric values, blank and the number 0 in it. If it finds the value it copies it to a LOG worksheet and deletes the row.

    If I run throught the code it does not remove all the 0's, blanks or text(#REF!)

    WHat am I doing wrong?

    [vba] With Worksheets(CSV)
    Last_Row = .Cells(.Rows.Count, "A").End(xlUp).Row
    j = 1
    For i = 1 To Last_Row
    If Not IsNumeric(.Cells(i, 3)) Or _
    Not IsNumeric(.Cells(i, 1)) Or _
    .Cells(i, 1) = "0" Or _
    .Cells(i, 1) = "" Then
    .Range("A" & i, "D" & i).Copy
    Worksheets("LOG").Range("A" & j).PasteSpecial Paste:=xlPasteValues
    j = j + 1
    .Rows(i).Delete
    End If
    Next i
    End With[/vba]

  2. #2
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    Hi mufasa,
    try looping through the rows from bottom to top in order not to skip a row after each row.delete takes place
    [vba]For i = Last_Row To 1 Step -1[/vba]
    Last edited by tstav; 04-08-2008 at 06:05 AM.
    He didn't know it was impossible, so he did it. (Jean Cocteau)

  3. #3
    Oops ... thanks 4 the info.

  4. #4
    VBAX Mentor tstav's Avatar
    Joined
    Feb 2008
    Location
    Athens
    Posts
    350
    Location
    You see, what happens is that when you are on row e.g. 5 and this row is deleted, the rest of the rows get shifted up one place, so now in row 5 you actually have row 6 and as if this were not enough, the code runs to the 'Next' clause of the For...Next loop and moves the row pointer to the next row #7. So you miss checking row #6.
    Stepping through the loop from the bottom up eliminates this drawback.
    He didn't know it was impossible, so he did it. (Jean Cocteau)

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •