+ Reply to Thread
Results 1 to 21 of 21

Need a macro to delete rows based on multiple conditions

  1. #1
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Need a macro to delete rows based on multiple conditions

    Hello!

    I just recently moved into a new job at work and one of my first duties is creating macros. I am woefully inept at this so far and for the most part have been reverse engineering recorded macros to learn.

    My problem I am looking at is that I have an array of six codes in COLUMN B that indicate reversals of payments. Once I see one of these codes in this column I need the macro to check the same row for a value in COLUMN C once it finds that value it needs to move up a row and check to see if the same value is in that row. If all three values are true it needs to delete both columns.

    Please Login or Register  to view this content.
    In this example it would delete rows 1,2,4,5 but leave row 3 intact.

    *This is the code I am currently attempting to use.*

    Please Login or Register  to view this content.
    Last edited by WorkAccount2; 03-23-2017 at 12:28 PM. Reason: Added missing CODE tags. Please read and follow the Forum Rules, link above in the menu bar. Thanks.

  2. #2
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    This is how I would run those tests, read it through and you'll see. The goal here is to delete all the rows at once at the end, so this should be much faster overall.
    Please Login or Register  to view this content.
    _________________
    Microsoft MVP 2010 - Excel
    Visit: Jerry Beaucaire's Excel Files & Macros

    If you've been given good help, use the icon below to give reputation feedback, it is appreciated.
    Always put your code between code tags. [CODE] your code here [/CODE]

    ?None of us is as good as all of us? - Ray Kroc
    ?Actually, I *am* a rocket scientist.? - JB (little ones count!)

  3. #3
    Valued Forum Contributor unit285's Avatar
    Join Date
    10-29-2015
    Location
    TN
    MS-Off Ver
    Office 365
    Posts
    358

    Re: Need a macro to delete rows based on multiple conditions

    Hello,

    Are "REV" and "6" the only codes you need to track? As is the only Reverse Codes?

  4. #4
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    No I have a total of six codes that would be 3 numbers each, so for instance they would be 200,201,202,203,204,REV. Thank you guys for the quick responses. I attempted to modify the code you posted for all six and the range failed. I assume this is because I tried to split each reversal code into it's own line like so.

    If Range("B" & Rw).Value = "REV" Or _
    Range("B" & Rw).Value = 200 Or_
    Range("B" & Rw).Value = 201 Or_
    Range("B" & Rw).Value = 202 Or_
    Range("B" & Rw).Value = 203 Or_
    Range("B" & Rw).Value = 204 Then

  5. #5
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    For that many, I'd switch to this syntax:
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Using that syntax I tried to change in my actual numbers which are 600-604 and it tells me the range failed. I cannot run option explicit as my previous macro uses an undefined variable in it's code. Sorry to be a pain.

  7. #7
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Just wondering if something seems off in the code placed below.

    The code is breaking on the second part. It is throwing an error of 'Method range of object _global failed.' for the second range in this line 'If Range("C" & Rw).Value = Range("C" & Rw - 1).Value Then'.

    If there's anything I missed let me know. Thanks so much for the help in advance!

    Please Login or Register  to view this content.

  8. #8
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    When it breaks, and you DEBUG, what is the value of Rw?

  9. #9
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    The value of Rw = 1 when it breaks.

  10. #10
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Alright I got it to actually work but it doesn't delete both rows like I originally intended. It delete's the row with the reversal code but not it's counterpart.

    To get it to work I had to change
    Please Login or Register  to view this content.
    I changed this to
    Please Login or Register  to view this content.

  11. #11
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    The value of RW should never be 1. The loop is running from the last row up to row 2 and stopping.

    What is the value of LR? That should always be higher than 2. If it's not, change the line of code above the loop where the LR variable is getting determined, choose a column other than "A".

  12. #12
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    I got the ranges defined but deleting the rows doesn't work still.

    This is the code I have:

    Please Login or Register  to view this content.
    Also, the value of LR is set as 5000, due to my table range.
    Last edited by WorkAccount2; 03-27-2017 at 02:33 PM. Reason: Forgot info.

  13. #13
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    Let's see this macro in a sample workbook not doing the right thing....

  14. #14
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    I'm unfortunately restricted from uploading it seems. But the problem it highlights is this portion of code now.
    Please Login or Register  to view this content.
    It deletes out the reversal code row but leaves the other one.

  15. #15
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    Try zipping the file you're trying to upload.

  16. #16
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Hopefully this works and makes sense.
    Attached Files Attached Files

  17. #17
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Here is the formatted version with the table.
    Attached Files Attached Files

  18. #18
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    Please Login or Register  to view this content.
    This code does what I want it to do but it also stops after it hits that first reversal code so I have to run it twice.

  19. #19
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    The presence of an Excel "Table" is gumming up the works. So, we test for that, remove the table if it's there, run the macro, delete the rows, then put the table back:
    Please Login or Register  to view this content.

  20. #20
    Registered User
    Join Date
    03-23-2017
    Location
    Harrisburg, Pennsylvania
    MS-Off Ver
    2010
    Posts
    12

    Re: Need a macro to delete rows based on multiple conditions

    This is perfect, thank you so much for all your hard work on this.

  21. #21
    Forum Expert JBeaucaire's Avatar
    Join Date
    03-21-2004
    Location
    Bakersfield, CA
    MS-Off Ver
    2010, 2016, Office 365
    Posts
    33,492

    Re: Need a macro to delete rows based on multiple conditions

    If that takes care of your original question, please select Thread Tools from the menu link above and mark this thread as SOLVED. Thanks.

+ Reply to Thread

Thread Information

Users Browsing this Thread

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

Similar Threads

  1. [SOLVED] Macro to delete rows based on multiple criteria
    By Andrew E Smith in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-12-2014, 12:17 PM
  2. identifying rows to delete based on multiple conditions
    By DannyGIS in forum Excel Formulas & Functions
    Replies: 10
    Last Post: 06-09-2014, 06:29 PM
  3. [SOLVED] Macro to delete multiple rows based on time of day
    By managermonroe in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-12-2013, 03:00 PM
  4. macro - delete rows based on multiple criteria.
    By ChocksterNo1 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-05-2013, 10:22 AM
  5. [SOLVED] Macro to Delete Rows based on Specific Conditions
    By stockgoblin42 in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 05-28-2013, 05:29 PM
  6. [SOLVED] Macro required to delete rows with multiple conditions
    By bigband1 in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 08-03-2012, 03:53 AM
  7. [SOLVED] VBA code to delete duplicate rows based on multiple conditions
    By pjsween in forum Excel General
    Replies: 5
    Last Post: 06-27-2012, 01:15 PM

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