Consulting

Results 1 to 5 of 5

Thread: Delete Row Skipping

  1. #1
    VBAX Regular
    Joined
    Feb 2010
    Posts
    41
    Location

    Delete Row Skipping

    Hello All. I cooked up the below code...

    Sub ForwardSettlementReport()
    
    Dim rng As Range, cell As Range, del As Range
    Set rng = Intersect(Range("D:D"), ActiveSheet.UsedRange)
    For Each cell In rng
        If (cell.Value) >= 0 _
        Or (cell.Value) = "-" Then
    Set del = cell
            del.Offset(0, -3).Select
            Range(Selection, Selection.End(xlToRight)).Select
            Selection.Delete Shift:=xlUp
    End If
    Next cell
    End Sub
    What it does is find all of the cells in column D that are >= 0 and, than, deletes the row leaving only the rows that have negative numbers in column D. However, it only deletes all the rows with positives after you run the code several times (typically 4-5). I can't figure out why the code ultimately deletes all the rows, but does not detect all of the positive numbers in just one run. Any help is much appreciated. Thanks.

    Charles

  2. #2
    Administrator
    VP-Knowledge Base
    VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Change your code to delete the last found value first

    e.g.
    For i = LastRow to 1 Step -1
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  3. #3
    VBAX Regular
    Joined
    Feb 2010
    Posts
    41
    Location
    How would that look in the code I've got? Still a VBA rookie...Thanks.

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    A few concepts in here, but try to follow it!

     
    Sub ForwardSettlementReport()
    Dim rng As Range, i%
        Set rng = Intersect(Range("D:D"), ActiveSheet.UsedRange)
    For i = rng.Cells.Count To 1 Step -1
        If (rng(i).Value) >= 0 _
            Or (rng(i).Value) = "-" Then
                rng(i).Offset(, -3).Resize(, 4).Delete Shift:=xlUp
            End If
        Next
    End Sub
    MVP (Excel 2008-2010)

    Post a workbook with sample data and layout if you want a quicker solution.


    To help indent your macros try Smart Indent

    Please remember to mark threads 'Solved'

  5. #5
    VBAX Regular
    Joined
    Feb 2010
    Posts
    41
    Location
    Thanks for the help.

Posting Permissions

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