+ Reply to Thread
Results 1 to 12 of 12

For next loop to delete empty row (advice my code)

  1. #1
    Forum Contributor
    Join Date
    03-12-2012
    Location
    Singapore
    MS-Off Ver
    MS 365
    Posts
    532

    For next loop to delete empty row (advice my code)

    Hi, i tried using for next loop to delete empty row. My code is as below

    Please Login or Register  to view this content.
    whenever i trigger the marco. My excel is like "hang". However, when i keep pressing ESC. It prompt the message saying the macro is being interupt. HOwever, then all my empty row has been deleted. What is wrong with my code?

  2. #2
    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 next loop to delete empty row (advice my code)

    When deleting rows you should go backwards. If you go forwards, if you are on row 5 and you delete it, then the row that used to be row 6 is now row 5, and you are going to skip that and go to row 6. It looks like you are trying to manage that by subtracting 1 from the index, but that is not a good practice.

    Try this. I would be happy to test it if you attach a file.
    Please Login or Register  to view this content.
    Jeff
    | | |·| |·| |·| |·| | |:| | |·| |·|
    Read the rules
    Use code tags to [code]enclose your code![/code]

  3. #3
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: For next loop to delete empty row (advice my code)

    Hello JP16,

    To delete rows using a loop you need to work from the bottom up
    Also, remove the counter (i = i - 1) as it is contradicting the Next i function.

    So your code should look like this:-

    Please Login or Register  to view this content.
    I hope that this helps.

    Cheerio,
    vcoolio.

  4. #4
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: For next loop to delete empty row (advice my code)

    Ooops! Sorry Jeff. I've done it again! Please remove my post as its the same reply as yours.

    Cheerio,
    vcoolio.

  5. #5
    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 next loop to delete empty row (advice my code)

    Quote Originally Posted by vcoolio View Post
    Ooops! Sorry Jeff. I've done it again! Please remove my post as its the same reply as yours.
    No reason to remove it, and we don't remove posts just because they recommend the same solution. Even if it is the same solution it is good to have explanations from different angles. Great minds think alike

  6. #6
    Valued Forum Contributor
    Join Date
    06-29-2014
    Location
    Australia
    MS-Off Ver
    MSO 365
    Posts
    1,098

    Re: For next loop to delete empty row (advice my code)

    No worries Jeff. Its hard to tell who else is working on the same thread until post time.

    Thanks again.

    vcoolio.

  7. #7
    Forum Contributor
    Join Date
    03-12-2012
    Location
    Singapore
    MS-Off Ver
    MS 365
    Posts
    532

    Re: For next loop to delete empty row (advice my code)

    Hi, please see attached. It will hang but eventually work if i press "ESC" to abort the code. why???
    So if i wan I to decrease, i have to add a Step -1?

    Anway, i thought my code consist of the i=i-1 which should address the problem u mention regarding deleting row?
    Attached Files Attached Files

  8. #8
    Registered User
    Join Date
    03-27-2018
    Location
    SG
    MS-Off Ver
    2013
    Posts
    6

    Re: For next loop to delete empty row (advice my code)

    The problem with your code is that you start with i=1, then with "i=i-1" i is now 0.
    Then when the loop repeats with next i, the loop starts again with i=1, hence a never ending loop process.

  9. #9
    Forum Contributor
    Join Date
    03-12-2012
    Location
    Singapore
    MS-Off Ver
    MS 365
    Posts
    532

    Re: For next loop to delete empty row (advice my code)

    Ya, i thot so too. But somehow, when i press ESC to abort. The end result is correct... funny....

  10. #10
    Registered User
    Join Date
    03-27-2018
    Location
    SG
    MS-Off Ver
    2013
    Posts
    6

    Re: For next loop to delete empty row (advice my code)

    I just noticed it, because the i=i-1 is in the if loop instead of the for loop, variable i will not -1 if the cell value is not blank. (you can see that for Cell(1,1) there is a value so it did not -1). Hence it will eventually get your answer but it will still stuck in a never ending loop

  11. #11
    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 next loop to delete empty row (advice my code)

    Quote Originally Posted by jp16 View Post
    Hi, please see attached. It will hang but eventually work if i press "ESC" to abort the code. why???
    So if i wan I to decrease, i have to add a Step -1?

    Anway, i thought my code consist of the i=i-1 which should address the problem u mention regarding deleting row?
    As I said, changing the index value inside a For loop is a poor programming practice. Did you try my version of the code?

  12. #12
    Forum Contributor
    Join Date
    03-12-2012
    Location
    Singapore
    MS-Off Ver
    MS 365
    Posts
    532

    Re: For next loop to delete empty row (advice my code)

    Ya, i tried, work perfect! Thanks! Anyway, does the "step -1" mean the "i" decrease by 1 everytime right? Then can we make it step +2 to make "i" increase by 2 everytime?

+ 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. Delete empty rows with VBA code
    By Motox in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 10-22-2015, 02:49 AM
  2. VBA code advice/Excel advice for summing groups of numbers
    By paulblower in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 07-17-2014, 05:47 AM
  3. [SOLVED] VBA Code for delete empty raws
    By Shihab in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 02-17-2014, 04:04 AM
  4. [SOLVED] Loop to delete empty rows needed
    By Smally in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 06-20-2013, 10:48 AM
  5. [SOLVED] still have empty cell after run Delete empty cells code
    By tuongtu3 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 11-25-2012, 04:28 PM
  6. need a code to delete empty cells
    By nasser in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 10-21-2008, 09:32 PM
  7. Loop all sheetsand delete empty rows
    By Sige in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-27-2005, 08:05 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