# Thread: [RESOLVED] nested For loop not working

1. ## [RESOLVED] nested For loop not working

I get an error message ' Next Without For' at the bottom of the second loop, and I have no idea why:-

For L = LBound(BArr4) To UBound(BArr4) 'For every line
TmpArr4() = Split(BArr4(L), " ") 'Split into words

For M = LBound(TmpArr4) To UBound(TmpArr4)
Tmp4 = TmpArr4(M)
A_Is = Left(Tmp4, 1)

Select Case A_Is
Case "A": Ac_1 = R
Case "X": Xc_1 = R
Case "T": Tmp4 = Tc_R
End Select

TmpArr4(M) = Tmp4
Next M

BArr4(L) = Join(TmpArr4, " ") 'Join words back into lines

If T_Value = "" Then T_Value = 12
If Ac_1 + Xc_1 >= T_Value Then

BArr4(L - 1) = Replace(BArr4(L - 1), Ac_1 & Xc_1, Tc_R)

TmpArr5(L) = Split(BArr4(L), "A")
TmpArr5(0) = XBlah

BArr4(L) = XBlah & "A" & Ac_1 & Xc_1 & vbNewLine & "A" & Tc_R

Next L

I get the error message even when I comment out all the code between "Next M" and "Next L", Which I can't understand. Any ideas?

2. ## Re: nested For loop not working

The problem is that you're missing an End If.

3. ## Re: nested For loop not working

I get an error message ' Next Without For' at the bottom of the second loop, and I have no idea why:-
you have not closed to your both End if .so try the following way .
Code:
```For L = LBound(BArr4) To UBound(BArr4) 'For every line
TmpArr4() = Split(BArr4(L), " ") 'Split into words

For M = LBound(TmpArr4) To UBound(TmpArr4)
Tmp4 = TmpArr4(M)
A_Is = Left(Tmp4, 1)

Select Case A_Is
Case "A": Ac_1 = R
Case "X": Xc_1 = R
Case "T": Tmp4 = Tc_R
End Select

TmpArr4(M) = Tmp4
Next M

BArr4(L) = Join(TmpArr4, " ") 'Join words back into lines

If T_Value = "" Then T_Value = 12
If Ac_1 + Xc_1 >= T_Value Then

BArr4(L - 1) = Replace(BArr4(L - 1), Ac_1 & Xc_1, Tc_R)

TmpArr5(L) = Split(BArr4(L), "A")
TmpArr5(0) = XBlah

BArr4(L) = XBlah & "A" & Ac_1 & Xc_1 & vbNewLine & "A" & Tc_R
End if
End if

Next L```

4. ## Re: nested For loop not working

If that's the case, why does it give the same error message when I comment all the code out?
Thanks for pointing it out though, I hadnt thought of it. So I'd need:-

If T_Value = "" Then T_Value = 12
End If
If Ac_1 + Xc_1 >= T_Value Then...
End If

5. ## Re: nested For loop not working

Because you have remove the statement when if becomes true .you need to do the statement .
Code:
```If T_Value = "" Then T_Value = 12

'Must have statement here when if condition is true
End If
If Ac_1 + Xc_1 >= T_Value Then...

'type your statement as well .
End If```

6. ## Re: nested For loop not working

Thanks, firoz.raj, no errors now, although whether the code works as it should is another matter...
But I'm still not sure why it gave the same error when all code between Next M and Next L were commented out, including all the unfinished if statements. It doesnt matter now, really, but I am still curious about it.

7. ## Re: nested For loop not working

The first If doesn't need and End If.
Code:
```For L = ...
...
For M = ...
...
Next M

If T_Value = "" Then T_Value = 12
If Ac_1 + Xc_1 >= T_Value Then
...
End If
Next L```
I suspect that you have another If statement preceding the loop that is also missing and End If statement.

8. ## Re: nested For loop not working

Yes, you're quite right! There's an If at the second line of the sub. Understand now.

#### Posting Permissions

• You may not post new threads
• You may not post replies
• You may not post attachments
• You may not edit your posts
•

Featured