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

How can you automate Excel?
Press Alt+F11 from Windows Excel to open the Visual Basic for Applications (VBA) editor.
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:


[TABLE="width: 521"]
<tbody>[TR]
[TD="align: left"]
Comparison

<tbody>
</tbody>
[/TD]
[TD]Index1[/TD]
[TD]Option 1[/TD]
[TD]Index2[/TD]
[TD]Option 2[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]1[/TD]
[TD]Vanilla[/TD]
[TD]2[/TD]
[TD]Choc[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]2[/TD]
[TD]Choc[/TD]
[TD]3[/TD]
[TD]Straw[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]3[/TD]
[TD]Straw[/TD]
[TD]1[/TD]
[TD]Vanilla[/TD]
[/TR]
</tbody>[/TABLE]

---
But mine looks like this - only the initial column is correct.
[TABLE="width: 521"]
<tbody>[TR]
[TD="align: left"]
Comparison

<tbody>
</tbody>
[/TD]
[TD]Index1[/TD]
[TD]Option 1[/TD]
[TD]Index2[/TD]
[TD]Option 2[/TD]
[/TR]
[TR]
[TD]1[/TD]
[TD]3[/TD]
[TD]Vanilla[/TD]
[TD]2[/TD]
[TD]Vanilla[/TD]
[/TR]
[TR]
[TD]2[/TD]
[TD]3[/TD]
[TD]Vanilla[/TD]
[TD]2[/TD]
[TD]Vanilla[/TD]
[/TR]
[TR]
[TD]3[/TD]
[TD]3[/TD]
[TD]Vanilla[/TD]
[TD]2[/TD]
[TD]Vanilla[/TD]
[/TR]
</tbody>[/TABLE]


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,223,952
Messages
6,175,593
Members
452,654
Latest member
mememe101

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