+ Reply to Thread
Results 1 to 12 of 12

VBA code: Doesnt delete all the values_Looping thru cells and deleting them

  1. #1
    Forum Contributor
    Join Date
    09-20-2014
    Location
    India
    MS-Off Ver
    2013
    Posts
    275

    Question VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    Hello All,

    I am using below code to loop through range of values in sheet2, in which first 3 letters of
    each value is a code.
    I have listed applicable codes in sheet1. With below code, i am trying to delete the values whose codes are
    not available in sheet1.
    I dont why, this codes doesn't delete all the values.

    Suppose if i have 150 values to be deleted(among 1000 values), after running below code, still 50 values remain as it is.
    Can any one figure out what the mistake i am doing here..? Why my code doesnt delete all the values..?

    Please Login or Register  to view this content.
    Last edited by mchilapur; 02-27-2017 at 07:17 AM.

  2. #2
    Valued Forum Contributor kasan's Avatar
    Join Date
    07-22-2009
    Location
    Riga, Latvia
    MS-Off Ver
    Excel 2010
    Posts
    680

    Re: VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    Hi,
    Are you sure you are looping through all cells you need?
    Is this correct range you define for loop?
    Please Login or Register  to view this content.

  3. #3
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,912

    Re: VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    Instead of using For Each cell, use an index and work up from the bottom. You're probably leap frogging cells as you delete the ones above.
    Trevor Shuttleworth - Retired Excel/VBA Consultant

    I dream of a better world where chickens can cross the road without having their motives questioned

    'Being unapologetic means never having to say you're sorry' John Cooper Clarke


  4. #4
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    Another point is do not loop sequentially through a range while deleting rows - loop in reverse order and reference the row number in the For ... Next loop.
    Last edited by cytop; 02-27-2017 at 06:59 AM.

  5. #5
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    You have a few range definitions that pull the end cell address from sheets different to the start cell address, which is difficult to follow. If you attach a file it will make it easier for us to analyse. A top of head guess is that it might be that you're deleting rows from the range being searched.
    MatrixMan.
    --------------------------------------
    If this - or any - reply helps you, remember to say thanks by clicking on *Add Reputation.
    If your issue is now resolved, remember to mark as solved - click Thread Tools at top right of thread.

  6. #6
    Forum Contributor
    Join Date
    09-20-2014
    Location
    India
    MS-Off Ver
    2013
    Posts
    275

    Re: VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    Hi Kasan,
    Sorry, i mis-typed it here..Its sheet2 only...

  7. #7
    Forum Contributor
    Join Date
    09-20-2014
    Location
    India
    MS-Off Ver
    2013
    Posts
    275

    Re: VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    Hello Cytop,
    Can you please provide me the code..?

  8. #8
    Forum Contributor
    Join Date
    09-20-2014
    Location
    India
    MS-Off Ver
    2013
    Posts
    275

    Re: VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    Hello MatrixMan,

    Due to confidential reasons, i cannot attach the original file..Sorry.

  9. #9
    Forum Contributor
    Join Date
    09-20-2014
    Location
    India
    MS-Off Ver
    2013
    Posts
    275

    Re: VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    Hello TMS,
    I think your guess is correct.. Now how to deal with it..? Please help

  10. #10
    Valued Forum Contributor
    Join Date
    09-19-2008
    Location
    It varies ...
    MS-Off Ver
    Office365 - 64bit
    Posts
    862

    Re: VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    Something like this?
    Please Login or Register  to view this content.

  11. #11
    Forum Expert
    Join Date
    02-14-2009
    Location
    .
    MS-Off Ver
    ................
    Posts
    2,840

    Re: VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    Due to confidential reasons, i cannot attach the original file
    for info, you're expected to take the time to anonymise a sample of the data...

  12. #12
    Forum Guru TMS's Avatar
    Join Date
    07-15-2010
    Location
    The Great City of Manchester, NW England ;-)
    MS-Off Ver
    MSO 2007,2010,365
    Posts
    44,912

    Re: VBA code: Doesnt delete all the values_Looping thru cells and deleting them

    Due to confidential reasons, i cannot attach the original file
    for info, you're expected to take the time to anonymise a sample of the data...
    If you need/want help, it is in your interests to help us help you. A sample file with some typical, though not actual/live, data will make the process much quicker and any solutions offered much more likely to work "out of the box".

    MatrixMan would be able to test his solution with your test data, update the sample and return it to you. So, not only would you have the code, but you would be able to see it working in the context of your data. But, your choice.

+ 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] Code to select and delete rows not deleting
    By Tonan230 in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 07-08-2016, 11:50 AM
  2. Delete Rows That Doesnt Contain Numbers in List
    By gurselgunacar in forum Excel Programming / VBA / Macros
    Replies: 14
    Last Post: 11-21-2013, 03:38 AM
  3. Changing code from deleting rows to cut/paste rows into another sheet and delete blank row
    By kmarshall6576 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 04-18-2013, 01:54 AM
  4. Problem deleting cells with this Code -
    By trickyricky in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 04-14-2011, 09:52 PM
  5. Delete row if value(array) doesnt exist in column
    By wobaby in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 06-24-2009, 02:34 AM
  6. Replies: 1
    Last Post: 06-09-2009, 03:15 PM
  7. VBA Code for deleting rows where cell in column a states "delete row"
    By tnfire in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 03-20-2009, 06:17 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