+ Reply to Thread
Results 1 to 5 of 5

For n=4 to lastrow Question

Hybrid View

  1. #1
    Registered User
    Join Date
    05-14-2013
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    78

    For n=4 to lastrow Question

    Hi everyone,

    I am having an issue with my loops.

    I am trying to go down column A and delete rows if they contain certain strings, such as "Test", "Individual", etc.

    My
    for n=4 to lastrow
    loop is working but when they are on consecutive rows, since the n counter will go up on the next, it will skip the second instance.

    So if i have rows

    example text
    Test
    example text
    Individual
    Test
    Example text

    It will skip the Test when it goes through the next iteration of the for loop.

    Can anyone explain how to stop it from doing that?
    Thanks for the help!

  2. #2
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: For n=4 to lastrow Question

    in any loops where you are deleting rows in your loop you need to count back

    in the "if loop" to determine what row to delete
    if condition is true and you actually delete a row you need to count back to N counter in order not to "skip" any rows

    reason for this is
    if n = 5 and it deletes the row
    the below row which would have been 6 becomes 5
    however n has incremented to 6
    therefore you have effectively skipped a row
    Last edited by humdingaling; 07-19-2013 at 04:03 AM. Reason: fixed explanation
    If you are satisfied with the solution(s) provided, please mark your thread as Solved.
    Select Thread Tools-> Mark thread as Solved. To undo, select Thread Tools-> Mark thread as Unsolved.

  3. #3
    Forum Guru MarvinP's Avatar
    Join Date
    07-23-2010
    Location
    Woodinville, WA
    MS-Off Ver
    Office 365
    Posts
    16,234

    Re: For n=4 to lastrow Question

    Hi Bruizer,

    Humdingaling is correct in his thinking but might need some cleanup on his explanation.

    If you read about a For.. Next Loop there is a third part that you need. It is the STEP part.

    You should do a
    For RowCtr = LastRow to 4 Step -1

    This will start at the lastrow and delete those rows without encountering the problem you've discovered.

    Look at http://www.databison.com/vba-for-loo...-each-in-next/ and the section that uses "Step".
    One test is worth a thousand opinions.
    Click the * Add Reputation below to say thanks.

  4. #4
    Forum Expert
    Join Date
    08-12-2012
    Location
    Sydney, Australia
    MS-Off Ver
    Excel 2010
    Posts
    5,636

    Re: For n=4 to lastrow Question

    yes sorry explanation was lacking

    reverse loop!
    genius

    thanks marvin

  5. #5
    Registered User
    Join Date
    05-14-2013
    Location
    California
    MS-Off Ver
    Excel 2013
    Posts
    78

    Re: For n=4 to lastrow Question

    Thanks guys, that worked! Cant believe I didnt think of that

+ 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] Sum Lastrow formula!!
    By Hurricanefly in forum Excel Programming / VBA / Macros
    Replies: 13
    Last Post: 05-17-2013, 11:35 AM
  2. Copy From LastRow to LastRow+7
    By dvent in forum Excel Programming / VBA / Macros
    Replies: 1
    Last Post: 02-11-2010, 06:56 AM
  3. lastrow of 3 columns
    By Directlinq in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 06-19-2009, 10:35 AM
  4. Dim Lastrow ? Dim LastColumn
    By khalid79m in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 01-10-2008, 01:43 PM
  5. Autofill to lastrow
    By Pedros in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 01-11-2007, 12:55 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