Consulting

Results 1 to 12 of 12

Thread: Sleeper: Delete row when condition is true

  1. #1
    VBAX Regular
    Joined
    Dec 2004
    Posts
    26
    Location

    Sleeper: Delete row when condition is true

    I am having problems with a project where the worksheet has 400 rows, if any cells in column "G" contain a specific word, then the whole row should be deleted.
    The rest of the macro works fine, I just can't seem to get this part right.

    Thanks for the help.

  2. #2
    Site Admin
    Urban Myth
    VBAX Guru
    Joined
    May 2004
    Location
    Oregon, United States
    Posts
    4,940
    Location
    Quote Originally Posted by outrider
    The rest of the macro works fine ..
    I'm worried by this line (before giving any advice). What do you mean exactly? Do you already have some worksheet event code? Is this part of a larger problem/solution?

  3. #3
    Administrator
    VP-Knowledge Base
    VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    When you are deleting rows you MUST care about the direction your deleting loop goes. If it goes from the smaller row to the bigger it WILL incur in error loosing some rows because when you delete a row, the next row goes up and then the loop advances, this makes it to not check a row. The right way is to go from the bigger to the smaller, something like:


    For i = Range("A65536").End(xlUp).Row To 1 Step - 1
    ... code ...
    Next

    Sure, as Zack said its hard to help with so little information but I would guess this might solve it.
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  4. #4
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Try the following

    Option Compare Text
    
    Sub TestIt()
    Dim ToFind As String, c As Range, MyRange As Range
    ToFind = "Test"
    With Intersect(Range("G:G"), ActiveSheet.UsedRange)
        Set c = .Find(ToFind, LookIn:=xlValues)
        If Not c Is Nothing Then
            firstAddress = c.Address
            Set MyRange = c.EntireRow
            Do
                Set c = .FindNext(c)
                Set MyRange = Union(MyRange, c.EntireRow)
    Loop While Not c Is Nothing And c.Address <> firstAddress
        End If
        MyRange.Delete
    End With
    End Sub

  5. #5
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    And here's the whole KB article, which may or may not use the same method that MD did. I'm no coder, so I didn't look.

    http://www.vbaexpress.com/kb/getarticle.php?kb_id=260
    ~Anne Troy

  6. #6
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi DB
    Very similar. I guess I'm needing to familiarise myself with these KB entries a bit better!

  7. #7
    Site Admin
    The Princess VBAX Guru Anne Troy's Avatar
    Joined
    May 2004
    Location
    Arlington Heights, IL
    Posts
    2,530
    Location
    Might save you a wee bit of time.
    ~Anne Troy

  8. #8
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    So outrider,

    is it solved?
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  9. #9
    VBAX Regular
    Joined
    Dec 2004
    Posts
    26
    Location

    Row Deletion.

    Quote Originally Posted by firefytr
    I'm worried by this line (before giving any advice). What do you mean exactly? Do you already have some worksheet event code? Is this part of a larger problem/solution?

    My system takes a large batch of customer orders, sorts them, adds data for parts, production times, materials etc using lookups from other worksheets, then separates the final data into two groups (Assemblies & single items) in a new workbook, this is where the problem starts.
    I knew that the deleting process would need to run backwards to ensure no rows would be missed, it just doesn't work properly yet.

  10. #10
    Administrator
    VP-Knowledge Base VBAX Master
    Joined
    Jan 2005
    Location
    Porto Alegre - RS - Brasil
    Posts
    1,219
    Location
    Can you post your macro, at least the part of it thats not working properly?
    Best Regards,

    Carlos Paleo.

    To every problem there is a solution, even if I dont know it, so this posting is provided "AS IS" with no warranties.

    If Debugging is harder than writing a program and your code is as good as you can possibly make
    it, then by definition you're not smart enough to debug it.




    http://www.mugrs.org

  11. #11
    Administrator
    VP-Knowledge Base VBAX Grand Master mdmackillop's Avatar
    Joined
    May 2004
    Location
    Scotland
    Posts
    14,489
    Location
    Hi Outrider,
    My suggested code does all the finds before deleting anything, and so does not run backwards. Is there any posibility of hidden characters which would prevent the Find from working?
    MD

  12. #12
    VBAX Regular
    Joined
    Dec 2004
    Posts
    26
    Location
    Adding code supplied by mdmackillop, I have finally got the thing to work.

    Thanks for all the help on this problem.

Posting Permissions

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