+ Reply to Thread
Results 1 to 6 of 6

changing value inside for loop which also is a criteria for the looping

Hybrid View

  1. #1
    Registered User
    Join Date
    11-19-2008
    Location
    Czech Republic
    MS-Off Ver
    365 at work / 2016 at home
    Posts
    24

    changing value inside for loop which also is a criteria for the looping

    Hello all,

    lets consider this:


    'looping from first row (1) to the last used row (10)
    For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row
    
        for j = 1 to 20
            'inserts 10 rows bellow where I am currently am (row i)
            i = i + 1
            ThisWorkbook.Worksheets("Report").Rows(i).Insert
            'do whatever i need here, so basicaly at this point the last used row from criteria now changed from 10 to 11, any by next loop (j) even more increased
        next j
    
    next i
    so at the poinf of first loop (i) the program stops
    so it does not update the for loop (for i) criteria properly, or as i desire.

    what would be the solution

  2. #2
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,903

    Re: changing value inside for loop which also is a criteria for the looping

    It doesn't run subsequent iterations of i, because by the time i = i + 1 has run 20 times in your For j loop then i is greater than you've asked it to loop to at the start, so it doesn't need to run any more.

    It's not actually clear, to me at least, what it is you're trying to achieve. Perhaps attach a workbook showing what your data looks like before and after and we can help more from there.

    BSB

  3. #3
    Registered User
    Join Date
    11-19-2008
    Location
    Czech Republic
    MS-Off Ver
    365 at work / 2016 at home
    Posts
    24

    Re: changing value inside for loop which also is a criteria for the looping

    ok, in simplified...
    A1 = "A"
    A2 = "B"
    A3 = "C"
    A4 = "D"

    so now i wish to loop through those for rows via:
    For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row

    and inside each loop i wish to insert extra 10 rows (via loop, not all 10 rows together) bellow the initial "i" and enter the numerical value 1 to 10

    so, slightly modified code to udnerstand:
    For i = 1 To Cells(Rows.Count, "A").End(xlUp).Row 'at starting point this value is = 4
    
        for j = 1 to 10
            i = i + 1
            Rows(i).Insert  ' this inserts one row bellow the letter (A/B/C/D)
            Cells(i, 1).Value = j
        next j
    
    next i
    so, you can see that begining of the first loop (i) it sets the criteria (i=1 to 4), but inside the first loop i create another loop which inserts 10 rows and therefore increases the "i" by +10... so after first loop of "i" I have fulfilled the initial criteria for the loop and it wont carry on to do the same under "B" value (which have shifted from row 2 to 11).

    And I need basicaly the program to insert 10 (one by one by another for loop "j") rows after each nonempty cell in column A

  4. #4
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,903

    Re: changing value inside for loop which also is a criteria for the looping

    Maybe like this?
    Sub AddRowsBetween()
        Dim i As Long: i = 1
        Dim j As Long
     
        Do Until Cells(i, 1) = ""
            For j = 1 To 10
                Cells(i + j, 1).EntireRow.Insert
                Cells(i + j, 1).Value = j
            Next j
            i = i + j
        Loop
    End Sub
    BSB

  5. #5
    Registered User
    Join Date
    11-19-2008
    Location
    Czech Republic
    MS-Off Ver
    365 at work / 2016 at home
    Posts
    24

    Re: changing value inside for loop which also is a criteria for the looping

    awesome,
    thank you

  6. #6
    Forum Expert BadlySpelledBuoy's Avatar
    Join Date
    06-14-2013
    Location
    East Sussex, UK
    MS-Off Ver
    365
    Posts
    7,903

    Re: changing value inside for loop which also is a criteria for the looping

    No problem. Happy to help

    BSB

+ 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] (Beginner help) For loop inside do loop that displays information from reference sheet.
    By lediable007 in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-08-2016, 03:20 PM
  2. [SOLVED] How to restrict pop of message box inside multiple times when it is inside a for loop?
    By timmu in forum Excel Programming / VBA / Macros
    Replies: 5
    Last Post: 06-26-2014, 06:00 AM
  3. Changing the index of a for loop inside of the loop
    By drinkmorewine in forum Excel Programming / VBA / Macros
    Replies: 10
    Last Post: 06-03-2013, 12:19 PM
  4. [SOLVED] Copy dynamically changing column and Paste using VBA Loop (Loop within Loop)
    By nixon72 in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 02-12-2013, 12:46 PM
  5. VBA For loop where number of iterations can be changed from inside loop
    By barrboy89 in forum Excel Programming / VBA / Macros
    Replies: 4
    Last Post: 05-11-2011, 12:18 PM
  6. For next loop carries on after criteria is met changing other cells????
    By Simon Lloyd in forum Excel Programming / VBA / Macros
    Replies: 6
    Last Post: 07-07-2006, 03:45 PM
  7. Looping SQL query w/changing parameters in each loop not working
    By Laurin in forum Excel Programming / VBA / Macros
    Replies: 7
    Last Post: 12-09-2005, 11:35 AM

Tags for this Thread

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