+ Reply to Thread
Results 1 to 14 of 14

Delete a group of rows with a condition

Hybrid View

ducky6 Delete a group of rows with a... 09-29-2010, 11:07 AM
pike Re: Delete a group of rows... 09-30-2010, 04:15 AM
ducky6 Re: Delete a group of rows... 09-30-2010, 08:45 AM
ducky6 Re: Delete a group of rows... 09-30-2010, 09:56 AM
pike Re: Delete a group of rows... 10-02-2010, 04:56 AM
ducky6 Re: Delete a group of rows... 10-03-2010, 07:08 AM
pike Re: Delete a group of rows... 10-03-2010, 07:20 AM
ducky6 Re: Delete a group of rows... 10-04-2010, 03:49 AM
davesexcel Re: Delete a group of rows... 10-04-2010, 06:59 AM
pike Re: Delete a group of rows... 10-04-2010, 07:18 AM
ducky6 Re: Delete a group of rows... 10-04-2010, 08:46 AM
davesexcel Re: Delete a group of rows... 10-04-2010, 11:03 AM
ducky6 Re: Delete a group of rows... 10-05-2010, 06:06 AM
davesexcel Re: Delete a group of rows... 10-18-2010, 09:31 PM
  1. #1
    Registered User
    Join Date
    08-23-2010
    Location
    Duckland
    MS-Off Ver
    Excel 2007
    Posts
    18

    Delete a group of rows with a condition

    Hi, everybody
    I use this macro to delete rows with a lower amount compared to the one specified by the user:

    Sub DeleteRows()
        Dim ur As Integer
        Dim limit$
        limit = InputBox("Delete rows with amount lower than:", "INSERT THE AMOUNT")
        With Sheets("mySheet")
            ur = .Cells(Rows.Count, 22).End(xlUp).Row
            For n = ur To 2 Step -1
                If .Cells(n, 22).Value < CLng(limit) Then
                    .Cells(n, 22).EntireRow.Delete
            End If
            Next n
        End With
        MsgBox ("Operation completed successfully")
    End Sub
    Now I need something more. Let's assume that those amounts are related to some client: I have to delete ALL the rows related to a client, if none of them reach the amount specified by the user.

    For example:
    Client A - 25
    Client A - 29
    Client A - 35
    Client B - 28
    Client B - 26
    Client B - 29
    If the limit is 30, I must keep all of rows for Client A, and delete all of rows for Client B.

    I hope my explanation was quite clear...
    Any help is welcome,
    thank you in advance!
    Last edited by davesexcel; 10-18-2010 at 09:29 PM.

  2. #2
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Delete a group of rows with a condition

    hi ducky6
    Why use code when Advance filter will do the trick.
    If the solution helped please donate to RSPCA

    Site worth visiting: Rabbitohs

  3. #3
    Registered User
    Join Date
    08-23-2010
    Location
    Duckland
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Delete a group of rows with a condition

    Hi, Pike
    thanks for your reply.

    I'm preparing some macro for people that are not Excel experts and requested me something automatic, that's why. Anyway I've already done most of the work...

    I'm trying to use a boolean variable inside the For loop... if the limit was reached by at least one row of a client, go back (with a While Do?) and delete all of rows for that client. Otherwise continue without deleting anything.
    But seems like I have some problem with nested loops...

    Thank you

  4. #4
    Registered User
    Join Date
    08-23-2010
    Location
    Duckland
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Delete a group of rows with a condition

    This is the code that I wrote so far:
    Sub DeleteRows()
        Dim ur As Integer
        Dim limReached As Boolean
        Dim limit$
        limit = InputBox("Delete rows with amount lower than:", "INSERT THE AMOUNT")
        client = ""
        With Sheets("mySheet")
            ur = .Cells(Rows.Count, 22).End(xlUp).Row
            For n = ur To 2 Step -1
                limReached = False
                If .Cells(n, 22).Value < CLng(limit) Then
                   limReached = True
                End If
                If Replace(Cells(n, 2), " ", "") <> Replace(client, " ", "") Then
                   If limReached = False Then
                      Do While (Replace(Cells(n, 2), " ", "") = Replace(client, " ", ""))
                      ActiveCell.Offset(1, 0).Select
                      ActiveCell.EntireRow.Delete
                      Loop
                   End If
                End If
                client = Cells(n, 2)
            Next n
        End With
        MsgBox ("Operation completed successfully")
    End Sub
    but it doesn't work... it doesn't delete any row and the message box appears almost immediately.
    Where am I wrong?

    Thank you

    EDIT: the variable limReached never changes to True, even when the limit is reached?
    Last edited by ducky6; 09-30-2010 at 10:48 AM.

  5. #5
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Delete a group of rows with a condition

    Hi ducky6,
    so any customer that has one value about the input value , has all records saved even it one is below the input value?

  6. #6
    Registered User
    Join Date
    08-23-2010
    Location
    Duckland
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Delete a group of rows with a condition

    Quote Originally Posted by pike View Post
    Hi ducky6,
    so any customer that has one value about the input value , has all records saved even it one is below the input value?
    Yes, that's right!
    It doesn't matter how many values (related to a customer) reach the limit, but if at least one row reach it or not.
    Limit reached --> keep all his records
    Limit not reached --> delete all his records

    This control must be done for all of customers.

    Thank you very much for your assistance!

  7. #7
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Delete a group of rows with a condition

    can you attach a sample worbook
    have to loop though list and test names , then add them to collection then delete if not in the list

  8. #8
    Registered User
    Join Date
    08-23-2010
    Location
    Duckland
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Delete a group of rows with a condition

    Sorry for late reply,
    please find attached a sample file with the macro that I'm trying to use.

    Of course the original file is much more complex, so I made a new one with simple data.

    I guess something is wrong with nested loops...
    Thank you very much
    Attached Files Attached Files

  9. #9
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Delete a group of rows with a condition

    Here is one way, it needs a couple of helper columns though.
    Attached Files Attached Files

  10. #10
    Forum Expert pike's Avatar
    Join Date
    12-11-2005
    Location
    Alstonville, Australia
    MS-Off Ver
    2016
    Posts
    5,342

    Re: Delete a group of rows with a condition

    not as sweat as daves
    but different
    Option Explicit
    Sub tester()
        Dim GoodCustomer As New Collection, YourValue As String, LastRow As Long, CellValue, GoodValue
        Dim xRow As Long, pItem
        YourValue = InputBox("Value", "value")
        LastRow = Cells(Rows.Count, 1).End(xlUp).Row
        For Each CellValue In Range("$A$3:$A" & LastRow)
            On Error Resume Next
            GoodCustomer.Add CellValue, CStr(CellValue)
        Next
        Rows("3:3").Select
        With Selection
            .AutoFilter
            LastRow = Cells(Rows.Count, 1).End(xlUp).Row
            .Range("$A$3:$D" & LastRow).AutoFilter Field:=2, Criteria1:=">=" & YourValue
            For Each GoodValue In Range("$A$3:$A" & LastRow).SpecialCells(12)
                On Error Resume Next
                GoodCustomer.Remove GoodValue
            Next
            .AutoFilter
        End With
        For xRow = 4 To LastRow
            For Each pItem In GoodCustomer
                If Cells(xRow, 1) = GoodCustomer.Item(pItem) Then
                    Cells(xRow, 3) = "X"
                End If
            Next pItem
        Next xRow
        Rows("3:3").Select
        With Selection
            .AutoFilter
            .Range("$A$3:$D" & LastRow).AutoFilter Field:=3, Criteria1:="<>X"
        End With
    End Sub

  11. #11
    Registered User
    Join Date
    08-23-2010
    Location
    Duckland
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Delete a group of rows with a condition

    First of all, thank you both!

    I checked your solutions and of course they work perfectly. This is exactly what I was trying to do.
    Both use helper columns, but it shouldn't be a big deal.

    I hope this topic will be useful for other users on this Forum.

    Thank you again!

    P.S. I just have a question for Daves: what RC[3], R1C5, C[1], RC[1], C[-1] are exactly?
    I assume they are references to the columns, but how can I change them if I need to refer to other columns? For example: AA and AB as helper columns, B as client and V as amount?
    Sorry but I'm really a newbie at macro...
    Last edited by ducky6; 10-04-2010 at 10:41 AM.

  12. #12
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Delete a group of rows with a condition

    Try this amended code, it is alot faster than the first example by not using the loop.
    Sub DeleteRows()
    
        Dim i As Long, LastRow As Long
    
    
    
        limite = InputBox("Delete rows with amount lower than:", "INSERT THE AMOUNT")
        Range("E1") = limite
        If Range("E1") = "" Then Exit Sub
        Columns("A:B").ClearContents
    
        Range("A2", Range("C65536").End(xlUp).Offset(0, -2)) = "=COUNTIF(RC[3]:RC[3],"">="" &R1C5)"
        Range("B2", Range("C65536").End(xlUp).Offset(0, -1)) = "=IF(SUMIF(C[1],RC[1],C[-1])=0,"""",1)"
        LastRow = Range("B" & Rows.Count).End(xlUp).Row
    
           With Range(Range("B2", Range("C65536").End(xlUp).Offset(0, -1)), Range("B2", Range("C65536").End(xlUp).Offset(0, -1)))
            .AutoFilter Field:=1, Criteria1:="="
            .EntireRow.Delete
        End With
    
    End Sub
    Attached Files Attached Files
    Last edited by davesexcel; 10-04-2010 at 11:06 AM.

  13. #13
    Registered User
    Join Date
    08-23-2010
    Location
    Duckland
    MS-Off Ver
    Excel 2007
    Posts
    18

    Re: Delete a group of rows with a condition

    Thank you so much Daves.
    Yes, this code is even quicker than the previous one

    But to tell you the truth, I don't understand yet how to changes the references (RC[3]:RC[3], R1C5, C[1], RC[1], C[-1]) to other columns...

    In my "real" file, AA and AB are supposed to be the helper columns, B is the client and V the amount. How can I refer to those columns?

    Thank you

  14. #14
    Forum Moderator davesexcel's Avatar
    Join Date
    02-19-2006
    Location
    Regina
    MS-Off Ver
    MS 365
    Posts
    13,525

    Re: Delete a group of rows with a condition

    Maybe attach a sample workbook that shows your real layout.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

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

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1