+ Reply to Thread
Results 1 to 6 of 6

Excel is skipping Rows as it works through my code.

  1. #1
    Registered User
    Join Date
    10-19-2020
    Location
    Bristol, England
    MS-Off Ver
    365 (& 2013)
    Posts
    83

    Excel is skipping Rows as it works through my code.

    I have found and doctored this code to do what I needed.

    BeginRow = 2
    EndRow = 30000
    checkcol = 4

    For Rowcnt = BeginRow To EndRow

    If Cells(Rowcnt, checkcol).Value = "Closed" Then
    Cells(Rowcnt, checkcol).EntireRow.Delete

    End If

    Next Rowcnt

    It works exactly as I needed it to, apart from the fact it misses Rows I need deleted. I could repeat it, and this does remove more, but not all, so I would need to repeat it many many times as there are thousands of rows.

    I cant work out why it misses the ones it does & then finds some of them on each consecutive pass.

    Any ideas?
    Last edited by CMOT Dibbler; 10-19-2020 at 10:38 AM.

  2. #2
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Excel is skipping Rows as it works through my code.

    Hi there,

    You need to work "from the bottom of the range to the top", otherwise the rows you delete will make keeping track of the current row number difficult.

    I haven't tested this fully but I think it will do what you need. Try replacing:

    Please Login or Register  to view this content.
    with:


    Please Login or Register  to view this content.

    Hope this helps,

    Greg M

  3. #3
    Forum Guru Norie's Avatar
    Join Date
    02-02-2005
    Location
    Stirling, Scotland
    MS-Off Ver
    Microsoft Office 365
    Posts
    19,643

    Re: Excel is skipping Rows as it works through my code.

    Whenever deleting rows you should loop from the top to the bottom.
    Please Login or Register  to view this content.
    If posting code please use code tags, see here.

  4. #4
    Registered User
    Join Date
    10-19-2020
    Location
    Bristol, England
    MS-Off Ver
    365 (& 2013)
    Posts
    83

    Re: Excel is skipping Rows as it works through my code.

    Thanks all, I can see why that would be the case in hindsight. Issue fixed.

  5. #5
    Forum Expert Greg M's Avatar
    Join Date
    08-16-2007
    Location
    Dublin. Ireland
    MS-Off Ver
    Office 2016
    Posts
    4,481

    Re: Excel is skipping Rows as it works through my code.

    Hi again,

    Many thanks for your very prompt feedback and also for the Reputation increase - much appreciated!

    You're welcome - glad I was able to help.

    Best regards,

    Greg M

  6. #6
    Forum Guru
    Join Date
    04-23-2012
    Location
    New Jersey, USA
    MS-Off Ver
    Excel 365
    Posts
    2,408

    Re: Excel is skipping Rows as it works through my code.

    Quote Originally Posted by Norie View Post
    Whenever deleting rows you should loop from the top to the bottom.
    Typo: Norie meant to write "...the bottom to the top" (the posted code correctly showed this).

+ 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. Code Breaks if only one line of data - works if multiple lines
    By snuffnchess in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 09-10-2020, 11:49 AM
  2. [SOLVED] VBA Skipping lines of code.
    By schulzy175 in forum Excel Programming / VBA / Macros
    Replies: 9
    Last Post: 02-17-2018, 07:41 PM
  3. [SOLVED] Is VBA skipping lines of code?
    By JonathanEngr in forum Excel Programming / VBA / Macros
    Replies: 3
    Last Post: 01-21-2018, 06:48 PM
  4. excel skipping code?
    By lazyengineer in forum Excel Programming / VBA / Macros
    Replies: 2
    Last Post: 07-14-2015, 02:26 PM
  5. [SOLVED] Macro skipping If statements when run but works in debug
    By peage1475 in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 01-13-2013, 01:20 AM
  6. [SOLVED] Sub is skipping lines of code after Then statement
    By chavez982 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 12-05-2012, 10:32 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