+ Reply to Thread
Results 1 to 6 of 6

For each x in y, delete row if match. Only deletes half of the matches?

  1. #1
    Registered User
    Join Date
    03-27-2013
    Location
    Long Island, NY
    MS-Off Ver
    Excel 2007
    Posts
    35

    Question For each x in y, delete row if match. Only deletes half of the matches?

    A user form pops up and is populated with all of the Vendors from the data in alphabetical order.
    The user then selects which Vendor(s) they would like to have their own worksheet in the workbook.
    The selected Vendor(s) then populate my "Vendor2" worksheet in alphabetical order.
    After it pulls the rows of the Vendor(s) into their own tabs and does some formatting, it then searches the "DM2" and "SC2" (also in alphabetical order) tabs for the selected Vendor(s) and deletes them from those sheets.

    However, it only deletes half of them. Say there are 4 occurances of the vendor in the "DM2" sheet, it will delete two of them. If you run it again, (now there are 2), it will delete one of them.
    If I keep running it, eventually they will be gone, but I do not want to have to run it over and over.

    I have used this method before and it has worked fine, however, I do not understand why it is doing this.

    Below is my code snippet. Any help would be greatly appreciated.



    Please Login or Register  to view this content.

  2. #2
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: For each x in y, delete row if match. Only deletes half of the matches?

    Are the cells getting missed consecutive?

    I would try looping through them backwards. I think you might be skipping the next row when one is deleted. If you delete row 102 the macro will not check it again even though new information (previously 103) shifted into it.
    Thanks,
    Solus


    Please remember the following:

    1. Use [code] code tags [/code]. It keeps posts clean, easy-to-read, and maintains VBA formatting.
    Highlight the code in your post and press the # button in the toolbar.
    2. Show appreciation to those who have helped you by clicking below their posts.
    3. If you are happy with a solution to your problem, mark the thread as [SOLVED] using the tools at the top.

    "Slow is smooth, smooth is fast."

  3. #3
    Registered User
    Join Date
    03-27-2013
    Location
    Long Island, NY
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: For each x in y, delete row if match. Only deletes half of the matches?

    Thank you Solus. Though as i got your reply i was about to put my new solution.

    instead of
    Please Login or Register  to view this content.
    i am using
    Please Login or Register  to view this content.
    and it works fine and accomplishes what it needs to (they just don't want to see them on the ohter sheets).

    I realized that once it is deleted, then the numbers are different. So it makes sense to have it be reversed.

  4. #4
    Administrator 6StringJazzer's Avatar
    Join Date
    01-27-2010
    Location
    Tysons Corner, VA, USA
    MS-Off Ver
    MS365 Family 64-bit
    Posts
    24,721

    Re: For each x in y, delete row if match. Only deletes half of the matches?

    I don't have time to set up a file that matches the setup I see in your code but the problem is that in your For loop you are deleting a row, so x doesn't point to a valid cell anymore. Then the For loop goes to the next cell, which ends up being the next cell if you had not deleted the first one. So it skips over a cell every time you do a delete.

    A way to avoid this is to use a Do While instead of a For loop, and use a row counter instead of a cell. Then if you delete a row, don't increment the row counter.

    Here is an example similar to yours which skips rows that it should delete, and another example that works properly:
    Please Login or Register  to view this content.
    I have used this method before and it has worked fine
    I'll bet you did something different.
    Jeff
    | | |會 |會 |會 |會 | |:| | |會 |會
    Read the rules
    Use code tags to [code]enclose your code![/code]

  5. #5
    Forum Expert Solus Rankin's Avatar
    Join Date
    05-24-2013
    Location
    Hollywood, CA
    MS-Off Ver
    Win7 Office 2010 VS Express 2012
    Posts
    2,655

    Re: For each x in y, delete row if match. Only deletes half of the matches?

    This is untested with no sample data but you might try this if you don't want to hide the rows.
    Please Login or Register  to view this content.

  6. #6
    Registered User
    Join Date
    03-27-2013
    Location
    Long Island, NY
    MS-Off Ver
    Excel 2007
    Posts
    35

    Re: For each x in y, delete row if match. Only deletes half of the matches?

    Thanks everyone, but I am happy with my own solution and marked the thread as solved once i put it up.

+ 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] INDEX MATCH array formula that matches substring n gives multiple matches
    By bkwins in forum Excel Formulas & Functions
    Replies: 4
    Last Post: 04-12-2013, 04:57 AM
  2. [SOLVED] Macro to delete rows with zero in column deletes row that contain 0 in any number
    By Bjordion in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-26-2012, 02:55 AM
  3. Replies: 4
    Last Post: 07-18-2012, 02:34 PM
  4. Replies: 0
    Last Post: 01-06-2008, 01:23 PM
  5. Replies: 1
    Last Post: 07-10-2006, 09:04 PM

Tags for this Thread

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