+ Reply to Thread
Results 1 to 12 of 12

how to change this for loop code to work from bottom up so it can delete rows

  1. #1
    Registered User
    Join Date
    06-13-2016
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    95

    how to change this for loop code to work from bottom up so it can delete rows

    hi,
    ive written this code to delete rows if Sheet 1 Column A matches Sheet 2 Column A (starting at row 12) and then if sheet 1 cell B5 also matches sheet2 column A.offset(0,1) (ie column B for corresponding row) to delete the rows from Sheet 1.
    What I realized (this is my first project writing extensive VBA so only just being exposed to such problems) is that the cell references in column A mess up when a row is deleted and so it skips it and goes to the next. Googling this told me that I need to code from the bottom up to not have this error.

    Im a little confused about the bottom up codes as its the first time ive seen it, can someone help me transform my code to reflect that?

    Please Login or Register  to view this content.

  2. #2
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: how to change this for loop code to work from bottom up so it can delete rows

    It should be the other for loop. For each loop will result in infinite loop.

    Please Login or Register  to view this content.

  3. #3
    Registered User
    Join Date
    06-13-2016
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    95

    Re: how to change this for loop code to work from bottom up so it can delete rows

    that makes things clearer

    So ive changed the code to this

    Please Login or Register  to view this content.
    I am getting an error on this row "If WS1.Range("B5") = c.Offset(0, 1) Then 'If ID#s in both sheets are the same for the seller" which i was not getting got going top bottm and everyhing was running smoothly except deleting every other row.
    Error says "Run-time error '91': Object vairable or with block variable not set"
    Is the syntax wrong in my line or do i need to define something else? How do I fix this

  4. #4
    Registered User
    Join Date
    06-13-2016
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    95

    Re: how to change this for loop code to work from bottom up so it can delete rows

    I realized I need t o change to Set c = data.Find(Cells(i, "A")) 'Find seller in MasterSheet since i is long

    So I did that and ran the code, but now if all the values exist in mastersheet, nothing in Add Request is getting deleted. I think my if logic syntax might be wrong so it thinks nothing matches both criteria?

  5. #5
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: how to change this for loop code to work from bottom up so it can delete rows

    Because c needs to be declared as a variable
    You are also searching for a partial match
    Please Login or Register  to view this content.

  6. #6
    Forum Contributor kalbasiatka's Avatar
    Join Date
    02-27-2016
    Location
    Brest, Belarus
    MS-Off Ver
    2021
    Posts
    224

    Re: how to change this for loop code to work from bottom up so it can delete rows

    the variant with the dictionary
    Attached Files Attached Files
    To do it for me and help me it is 2 different things!
    Sorry for my english, blame Google translator

  7. #7
    Registered User
    Join Date
    06-13-2016
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    95

    Re: how to change this for loop code to work from bottom up so it can delete rows

    AB33 - Yeah I realized that and changed it too, but, for some reason it is not deleting any rows now even if both criteria exist in mastersheet. Is my if clause wrong in syntax?

  8. #8
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: how to change this for loop code to work from bottom up so it can delete rows

    Please attach a sample to see why your code is not working.

  9. #9
    Registered User
    Join Date
    06-13-2016
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    95

    Re: how to change this for loop code to work from bottom up so it can delete rows

    Sure here is a sample
    Attached Files Attached Files

  10. #10
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: how to change this for loop code to work from bottom up so it can delete rows

    Are the match exact or part match?

    Please Login or Register  to view this content.

  11. #11
    Registered User
    Join Date
    06-13-2016
    Location
    NJ
    MS-Off Ver
    2010
    Posts
    95

    Re: how to change this for loop code to work from bottom up so it can delete rows

    it should be exact

  12. #12
    Forum Expert
    Join Date
    03-28-2012
    Location
    TBA
    MS-Off Ver
    Office 365
    Posts
    12,454

    Re: how to change this for loop code to work from bottom up so it can delete rows

    I have now adjusted and it works. I have also attached my own code. Both codes gives you a slightly different result. If there are duplicates, my code picks up the second duplicate while your code picks up the first duplicates.
    Attached Files Attached Files

+ 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] How do I delete blank rows at the bottom of my data
    By Interesting Ian in forum Excel General
    Replies: 7
    Last Post: 11-17-2014, 04:59 PM
  2. [SOLVED] VB Code To Delete Rows between 2 flags on loop
    By shauncol3 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-08-2012, 07:46 PM
  3. [SOLVED] Excel 2007 : Blank Rows At Bottom of Sheet Will Not Delete
    By KSSLR in forum Excel General
    Replies: 3
    Last Post: 06-20-2012, 01:15 PM
  4. change a code to delete rows
    By meslija in forum Excel Programming / VBA / Macros
    Replies: 0
    Last Post: 05-06-2011, 04:53 PM
  5. delete bottom 6 rows
    By adam2308 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-01-2009, 07:52 PM
  6. delete extra rows at bottom
    By morgonzola in forum Excel General
    Replies: 4
    Last Post: 02-02-2007, 12:50 AM
  7. [SOLVED] How do I delete blank rows at the bottom of a spreadsheet to get .
    By Miklaurie in forum Excel General
    Replies: 1
    Last Post: 01-26-2005, 11:06 AM

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