+ Reply to Thread
Results 1 to 23 of 23

delete row based on criteria of value in two columns

  1. #1
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    delete row based on criteria of value in two columns

    I'm trying to write a vba that will delete a row based on the criteria of two columns. In this case if the value of the two columns is zero (or null) or the value is an error, I want the row to be deleted. I tried the following code, but nothing seemed to happen. Your help is greatly appreciated.

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    07-15-2012
    Location
    Leghorn, Italy
    MS-Off Ver
    Excel 2010
    Posts
    3,431

    Re: delete row based on criteria of value in two columns

    Please Login or Register  to view this content.
    If solved remember to mark Thread as solved

  3. #3
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: delete row based on criteria of value in two columns

    The code you provide worked ok, but I made an error in my initial post. The portion of the code that pertains to equal zero = (Range("e" & i).Formula = 0 And Range("f" & i).Formula = 0), should actully be if the cell is blank. Can you assist?

  4. #4
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: delete row based on criteria of value in two columns

    Maybe this

    Please Login or Register  to view this content.
    Thanks,
    Mike

    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved.

  5. #5
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: delete row based on criteria of value in two columns

    Mike the code seems to stop running at the "End If" statement. Do you know why?

  6. #6
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: delete row based on criteria of value in two columns

    Not sure what you mean? Stop running?

    Code look from rows 10 to 3 and if BOTH Column E and Column F = "" then it will delete the row.

  7. #7
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: delete row based on criteria of value in two columns

    When running the macro the vba editor opens and the "End if" line is highlighted.

  8. #8
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: delete row based on criteria of value in two columns

    Are you sure you have copied th ecode correctly?

  9. #9
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: delete row based on criteria of value in two columns

    Yup, copied it clean (twice). Still get a debug error at "End if".

  10. #10
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: delete row based on criteria of value in two columns

    Maybe add .value to the end

    Please Login or Register  to view this content.

    Remove the ) before the Then

    Please Login or Register  to view this content.
    Last edited by mike7952; 09-16-2012 at 03:06 PM.

  11. #11
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: delete row based on criteria of value in two columns

    Still a no go. I'll have to look at it more later. Thanks for your help anyway.

  12. #12
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: delete row based on criteria of value in two columns

    Ok I missed this before but try this

    Please Login or Register  to view this content.

  13. #13
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: delete row based on criteria of value in two columns

    This code works great, although it is slow since the actual sheet I'm running it on has 1600 rows. I still need to add the "Or IsError(Range("e" & i)) ", to make it more fault tolerant. Rather than deleting the rows one at a time, would it be faster to select the rows and then delete them all at once at the end.

  14. #14
    Forum Expert mike7952's Avatar
    Join Date
    12-17-2011
    Location
    Florida
    MS-Off Ver
    Excel 2007, Excel 2016
    Posts
    3,520

    Re: delete row based on criteria of value in two columns

    Give this a try

    Please Login or Register  to view this content.

  15. #15
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: delete row based on criteria of value in two columns

    I wonder if it would be possible to do a filter one formulas that don't meet this criteria, "Range("e" & i).Value > 0 or Range("f" & i).Value > 0)", that way all the blank rows or error rows would be filtered and a deletion of the selected range could be performed.

  16. #16
    Registered User
    Join Date
    07-27-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007/2010
    Posts
    86

    Re: delete row based on criteria of value in two columns

    Hi tomtam
    See if this works better
    Please Login or Register  to view this content.
    Opps.. There's a solution alr.
    Last edited by bheanloh; 09-16-2012 at 07:15 PM.
    Boon

  17. #17
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: delete row based on criteria of value in two columns

    Hi tomham,

    Try this (initially on a copy of your data as the results cannot be undone if they're not as expected) which is quite quick as it doesn't use looping:

    Please Login or Register  to view this content.
    HTH

    Robert
    ____________________________________________
    Please ensure you mark your thread as Solved once it is. Click here to see how
    If this post helps, please don't forget to say thanks by clicking the star icon in the bottom left-hand corner of my post

  18. #18
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: delete row based on criteria of value in two columns

    This code worked fast, but got hung up on the error rows. The vba error was "runtime error '13' type mismatch". In the example I'm testing is divide by zero error, but it could other errors. The sheet the code is working on is actually referencing another sheet.

  19. #19
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: delete row based on criteria of value in two columns

    I tried this Robert, but it didn't work correctly. The end result shows the correct number of rows, but the results seem random. I only want to keep rows that have values > 0 in columns E & F.

  20. #20
    Registered User
    Join Date
    07-27-2012
    Location
    Malaysia
    MS-Off Ver
    Excel 2007/2010
    Posts
    86

    Re: delete row based on criteria of value in two columns

    Both columns E & F contain > 0, or either one of them contains > 0?

  21. #21
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: delete row based on criteria of value in two columns

    I am using a formula to show "" if a zero is encountered, so it would be if either column E > 0 or F > 0. To answer your question, "either one".

  22. #22
    Registered User
    Join Date
    09-14-2012
    Location
    Maple Ridge, BC
    MS-Off Ver
    Excel 2010
    Posts
    18

    Re: delete row based on criteria of value in two columns

    I change my sheet to show errors as "", so the code Mike provided earlier will work for my purpose.

    Please Login or Register  to view this content.
    [SOLVED]

  23. #23
    Forum Expert
    Join Date
    12-10-2006
    Location
    Sydney
    MS-Off Ver
    Office 365
    Posts
    3,527

    Re: delete row based on criteria of value in two columns

    The sheet the code is working on is actually referencing another sheet
    That's a key piece of information!! What's the sheet name?

+ 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