+ Reply to Thread
Results 1 to 14 of 14

Delete a group of rows with a condition

  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:

    Please Login or Register  to view this content.
    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:
    Please Login or Register  to view this content.
    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
    Please Login or Register  to view this content.

  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.
    Please Login or Register  to view this content.
    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