Nested Loop Not Populating Down the Rows

CatLadee

New Member
Joined
Sep 7, 2018
Messages
29
Hi all,

Any ideas why this nested loop is staying on just one row and not pulling down? Thanks in advance - CatLadee

Code:
r = 1
[COLOR=#00007F][FONT=Calibri]For[/FONT][/COLOR][COLOR=#3F3F3F][FONT=Calibri] intOuter = 1 [/FONT][/COLOR][COLOR=#00007F][FONT=Calibri]To[/FONT][/COLOR][COLOR=#3F3F3F][FONT=Calibri] intOptions - 1[/FONT][/COLOR]

[COLOR=#00007F][FONT=Calibri]For[/FONT][/COLOR][COLOR=#3F3F3F][FONT=Calibri] intInner = intOuter + 1 [/FONT][/COLOR][COLOR=#00007F][FONT=Calibri]To[/FONT][/COLOR][COLOR=#3F3F3F][FONT=Calibri] intOptions[/FONT][/COLOR]
[COLOR=#00007F][FONT=Calibri]       With[/FONT][/COLOR][COLOR=#3F3F3F][FONT=Calibri] Range("Output_Tbl")[/FONT][/COLOR]
[COLOR=#3F3F3F][FONT=Calibri]                .Offset(r, 0).Value = intPairs[/FONT][/COLOR]
[COLOR=#3F3F3F][FONT=Calibri]                .Offset(r, 1).Value = intOuter[/FONT][/COLOR]
[COLOR=#3F3F3F][FONT=Calibri]                .Offset(r, 2).Value = arrOptions(r)[/FONT][/COLOR]
[COLOR=#3F3F3F][FONT=Calibri]                .Offset(r, 3).Value = intInner[/FONT][/COLOR]
[COLOR=#3F3F3F][FONT=Calibri]                .Offset(r, 4).Value = arrOptions(r)[/FONT][/COLOR]
[COLOR=#00007F][FONT=Calibri]       End [/FONT][/COLOR][COLOR=#00007F][FONT=Calibri]With[/FONT][/COLOR]
[COLOR=#00007F][FONT=Calibri]Next[/FONT][/COLOR][COLOR=#3F3F3F][FONT=Calibri] intInner[/FONT][/COLOR]

[COLOR=#00007F][FONT=Calibri]Next[/FONT][/COLOR][COLOR=#3F3F3F][FONT=Calibri] intOuter[/FONT][/COLOR]

Next intOuter
 

Excel Facts

Round to nearest half hour?
Use =MROUND(A2,"0:30") to round to nearest half hour. Use =CEILING(A2,"0:30") to round to next half hour.
I've changed it to this - same problem :(

Code:
  r = 1              
    With Range("Output_Tbl")
    For intOuter = r To intOptions - r '1 to 3
        For intInner = intOuter + r To intOptions
            
                .Offset(r, 0).Value = intPairs
                .Offset(r, 1).Value = intOuter
                .Offset(r, 2).Value = arrOptions(r)
                .Offset(r, 3).Value = intInner
                .Offset(r, 4).Value = arrOptions(r)
            
        Next intInner
    
    Next intOuter
    End With
 
Upvote 0
You are setting the variable 'r' to 1 before the loop starts, then nowhere inside the loops do you ever change the value of 'r', so it remains 1 for each iteration of the loops. I am not entirely sure what you are ultimately trying to do, but that is at the heart of your question/problem.
 
Last edited:
Upvote 0
You are setting the variable 'r' to 1 before the loop starts, then nowhere inside the loops do you ever change the value of 'r', so it remains 1 for each iteration of the loops. I am not entirely sure what you are ultimately trying to do, but that is at the heart of your question/problem.

Thanks, Rick! I added an r loop that fixed part of the issue but it's still not clicking.

This is supposed to pull a string of text from a CSV file with a short list of items and compare them in the output to a worksheet like this:


Comparison

<tbody>
</tbody>
Index1Option 1Index2Option 2
11Vanilla2Choc
22Choc3Straw
33Straw1Vanilla

<tbody>
</tbody>

---
But mine looks like this - only the initial column is correct.
Comparison

<tbody>
</tbody>
Index1Option 1Index2Option 2
13Vanilla2Vanilla
23Vanilla2Vanilla
33Vanilla2Vanilla

<tbody>
</tbody>


Code:
Open fileName For Input As [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL]     Line Input [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] , dataLine
    
    arrOptions = Split(dataLine, ", ")
      
    Close [URL=https://www.mrexcel.com/forum/usertag.php?do=list&action=hash&hash=1]#1[/URL] 
                  
    For r = LBound(arrOptions) To UBound(arrOptions)
        
        intOptions = r + 1
    
    Next r
           
   
    intPairs = (intOptions - 1) * (intOptions / 2)
    
    With Range("Output_Tbl")
        For r = 1 To intOptions
            For intOuter = 1 To intOptions - 1 ' DOESNT WORK '1 to 3
                For intInner = 1 + 1 To intOptions '2 to 4
        
                    .Offset(r, 0).Value = r 'Only one that Works
                    .Offset(r, 1).Value = intInner
                    .Offset(r, 2).Value = arrOptions()
                    .Offset(r, 3).Value = intOuter
                    .Offset(r, 4).Value = arrOptions()
                
                Next intInner
             Next intOuter
        Next r
    End With
 
Upvote 0

Forum statistics

Threads
1,214,945
Messages
6,122,397
Members
449,081
Latest member
JAMES KECULAH

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Back
Top