# Problems with outer loop array

#### kylefoley76

##### Well-known Member
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

Copy PDF to Excel
Select data in PDF. Paste to Microsoft Word. Copy from Word and paste to Excel.

#### Andrew Poulsom

##### MrExcel MVP
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``````

#### ukmikeb

##### Well-known Member
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

#### kylefoley76

##### Well-known Member
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.

#### ukmikeb

##### Well-known Member
Thanks for the feedback.

Replies
13
Views
457
Replies
4
Views
104
Replies
14
Views
255
Replies
5
Views
140
Replies
22
Views
831

### Forum statistics

1,191,123
Messages
5,984,774
Members
439,910
Latest member
Flyingjoblo ### 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.

### Which adblocker are you using?    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

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