Consulting

Results 1 to 3 of 3

Thread: Solved: Expression not running all the way through

  1. #1
    VBAX Mentor
    Joined
    Aug 2011
    Posts
    353
    Location

    Solved: Expression not running all the way through

    I have this expression to delete the rows in which the cell in column D has the value "0" in it.

    However this expression only deletes some of the rows where cell D is "0".

    Do you see anything wrong with it?

    [VBA]Sub tester2()

    Dim lastrow As Long
    Dim i As Long
    With ActiveSheet

    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = 2 To lastrow

    If .Cells(i, "D").Value = "0" Then

    With Range(.Cells(i, "A"), .Cells(i, "X")).Select
    Selection.Delete
    End With

    End If

    Next i

    End With

    End Sub
    [/VBA]

  2. #2
    Mac Moderator VBAX Guru mikerickson's Avatar
    Joined
    May 2007
    Location
    Davis CA
    Posts
    2,778
    When deleting rows, one has to work from the bottom up
    [VBA]For i = lastrow To 2 Step -1[/VBA]

  3. #3
    VBAX Mentor
    Joined
    Aug 2011
    Posts
    353
    Location
    [VBA]Sub tester2()

    Dim lastrow As Long
    Dim i As Long
    With ActiveSheet

    lastrow = .Cells(.Rows.Count, "A").End(xlUp).Row
    For i = lastrow To 2 Step -1

    If .Cells(i, "D").Value = "0" Then

    With Range(.Cells(i, "A"), .Cells(i, "X")).Select
    Selection.Delete Shift:=xlUp
    End With

    End If

    Next i

    End With

    End Sub[/VBA]

    that works great, thanks!!!

Posting Permissions

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