Problems with outer loop array

kylefoley76

Well-known Member
Joined
Mar 1, 2010
Messages
1,553
I have two loops each containing the same members. During the course of the loop, the size of the array gets increased. However, only the inner loop is being increased. The outer loop also increases but VBA still exits the loop before the upper bound is being reached. Here is a scheme of what is happening.

For i = 1 to ubound(tsent)

For j = 1 to ubound(tsent)

if a then
c = UBound(tsent) + 1 ReDim Preserve tsent(c)
tsent(c) = new_atomic
end if
next j
next i

Excel will eventually loop up to j = 12, but it won't loop pass i = 5 which is size of the array at the beginning. I tried creating two different arrays but that didn't help matters much. Here are some excerpts from the code, the irrelevant parts deleted:

Code:
For t = 1 To UBound(sent)


        temp_str = sent(t)
        temp_relat = find_relat(temp_str)
        relat = temp_relat(1)
        obj = temp_relat(2)
        subj = temp_relat(3)
        positive = temp_relat(42)
        transitive = temp_relat(6)
       If transitive And positive = False Then
       w = w + 1
       ReDim Preserve tsent(w)
       ReDim Preserve trsent(w)
       tsent(w) = temp_str
       trsent(w) = temp_str
       
       
        End If




Next






    For i = 1 To UBound(trsent)
        
        
        temp_str = trsent(i)
        temp_relat = find_relat(temp_str)
        relat = temp_relat(1)
        obj = temp_relat(2)
        subj = temp_relat(3)
        positive = temp_relat(42)
        transitive = temp_relat(6)
       prop_variable = temp_relat(45)
        
       
         
            For j = 1 To UBound(tsent) Step 1
                
                temp_str2 = tsent(j)
                temp_relat2 = find_relat(temp_str2)
                relat2 = temp_relat2(1)
                obj2 = temp_relat2(2)
                subj2 = temp_relat2(3)
                positive2 = temp_relat2(42)
            
               
                If relat = relat2 And obj = subj2 Then
                 
                                    
                   
  
                        
                        If Not IsInArray(new_atomic, tsent) Then
                            c = UBound(tsent) + 1
                            ReDim Preserve tsent(c)
                            ReDim Preserve trsent(c)
                            tsent(c) = new_atomic
                            trsent(c) = new_atomic
                            
                            
                            
                            [deleted code]
       
                                
                                    
                End If
            End If
            
        Next j
    Next i
 






Next
 

Excel Facts

What is the fastest way to copy a formula?
If A2:A50000 contain data. Enter a formula in B2. Select B2. Double-click the Fill Handle and Excel will shoot the formula down to B50000.
UBound(tsent) is evaluated only once. It isn't reevaluated as each iteration. Demonstration:

Code:
Sub Test()
    Dim i As Long
    Dim Arr() As Long
    ReDim Arr(1 To 1)
    For i = 1 To UBound(Arr)
        ReDim Preserve Arr(1 To UBound(Arr) + 1)
        MsgBox "i: " & i & " " & "UBound: " & UBound(Arr)
    Next i
End Sub
 
Upvote 0
Hi

Try -
Code:
i=0:j=0

Do  '  i  loop start
i = i + 1
 
Do '  j  loop start
j = j + 1
 if a then
 c = UBound(tsent) + 1 ReDim Preserve tsent(c)
 tsent(c) = new_atomic
 end if 
 
Loop until j = ubound(tsent) ' j loop end 
Loop until i = ubound(tsent) ' i loop end

hth
 
Upvote 0
Hi

Try -
Code:
i=0:j=0

Do  '  i  loop start
i = i + 1
 
Do '  j  loop start
j = j + 1
 if a then
 c = UBound(tsent) + 1 ReDim Preserve tsent(c)
 tsent(c) = new_atomic
 end if 
 
Loop until j = ubound(tsent) ' j loop end 
Loop until i = ubound(tsent) ' i loop end

hth
Thanks, that solved things.
 
Upvote 0
Thanks for the feedback.

Pleased to have helped solve your problem.

Good luck with your project.
 
Upvote 0

Forum statistics

Threads
1,213,565
Messages
6,114,338
Members
448,570
Latest member
rik81h

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