Compile Error: Next without For
Hi,
I am a novice at this and would appreciate any help.
I get a Compile Error: Next without For message even though I do have a For. Here's the script:
Dim myOscArray(10, 3) As Single
Dim mySumflow As Single
Dim myMeanflow As Single
mySumflow = 0
j = 0
Set currentCell = Range("I11")
For i = 1 To myDatasecs - 2
Set nextCell = currentCell.Offset(1, 0)
Set currentCell = nextCell
mySumflow = mySumflow + currentCell.Offset(0, -2).Value
If j = 11 Then
myMeanflow = mySumflow / i
Exit For
Else
If currentCell.Formula <> "" Then
myOscArray(j, 1) = currentCell.Value
Else
If currentCell.Offset(0, 1).Value <> "" Then
myOscArray(j, 2) = currentCell.Value
j = j + 1
Else
Next i
Thanks,
Neil
Re: Compile Error: Next without For
Quote:
Originally Posted by neilma
Hi,
Code:
Dim myOscArray(10, 3) As Single
Dim mySumflow As Single
Dim myMeanflow As Single
mySumflow = 0
j = 0
Set currentCell = Range("I11")
For i = 1 To myDatasecs - 2
Set nextCell = currentCell.Offset(1, 0)
Set currentCell = nextCell
mySumflow = mySumflow + currentCell.Offset(0, -2).Value
If j = 11 Then
myMeanflow = mySumflow / i
Exit For
Else
If currentCell.Formula <> "" Then
myOscArray(j, 1) = currentCell.Value
Else
If currentCell.Offset(0, 1).Value <> "" Then
myOscArray(j, 2) = currentCell.Value
j = j + 1
Else
End if
end if
Next i
You forgot to close your if statements
Re: Compile Error: Next without For
Thanks for the help.
I guess it's obvious that I don't know what I'm doing.
I have another problem, but I'll post it on another thread since it doesn't fit this title.
Thanks.
Neil
Re: Compile Error: Next without For
I am having trouble with a piece of code I recently wrote. It is coming up with a compile error: next without For.
Thanks!!!
While Not records.EOF
For i = 0 To 6
If i = 5 Then
i = 8
ElseIf i = 6 Then
i = 11
If isPlanLocked(records!Year, i) Then
Forms!frmMain!frameDivision.value = i
DoCmd.OpenForm "frmPlan"
Forms!frmPlan!txtYear.value = records!Year
DoCmd.OpenForm "frmPrintPlan"
PrintPlan
DoCmd.OutputTo acOutputReport, "rptPlan", "Snapshot Format", path & Forms!frmPlan!txtYear.value & " " & FindDivisionName(Forms!frmMain!frameDivision.value) & ".snp"
DoCmd.Close acReport, "rptPlan"
DoCmd.Close acForm, "frmPrintPlan"
DoCmd.Close acForm, "frmPlan"
End If
records.MoveNext
Next i
Wend
records.Close
Re: Compile Error: Next without For
The error is because you are missing an End If. See below...
vb Code:
While Not records.EOF
For i = 0 To 6
If i = 5 Then
i = 8
ElseIf i = 6 Then
i = 11
End If '<~~~ YOU ARE MISSING THIS
If isPlanLocked(records!Year, i) Then
Forms!frmMain!frameDivision.Value = i
DoCmd.OpenForm "frmPlan"
Forms!frmPlan!txtYear.Value = records!Year
DoCmd.OpenForm "frmPrintPlan"
PrintPlan
DoCmd.OutputTo acOutputReport, "rptPlan", _
"Snapshot Format", Path & Forms!frmPlan!txtYear.Value & _
" " & FindDivisionName(Forms!frmMain!frameDivision.Value) & ".snp"
DoCmd.Close acReport, "rptPlan"
DoCmd.Close acForm, "frmPrintPlan"
DoCmd.Close acForm, "frmPlan"
End If
records.MoveNext
Next i
Wend
records.Close
Tip: If you arrange your code like the way I did, then it is always easier to read them and keep a track of your ifs,endif's,for,next etc...
Re: Compile Error: Next without For
1) even though it's semi-related... it would have been better to start a new thread than to dredge up a 4 year old one --- But KUDOS for using the search (as that's the only way this would have shown up)
2) The code snippet is hard to read, if you can repost it, using [code][/code] tags around it, it'll preserve the indents, making it easier to read.
3) Oddly enough, your problem is the same as the original thread... you've got an IF statement that is never closed off... EVERYTIME you have an if, you should have an END IF to go with it.
-tg
Re: Compile Error: Next without For
Re: Compile Error: Next without For
im having same error but why ?
Code:
Dim Geçici(28),Kalıcı(28) as integer
Public Sub RandomIndis()
Dim i, k As Integer
Geçici(0) = RandomSayi(0, 80)
Kalıcı(0) = Geçici(0)
i = 1
While (i < 28)
Geçici(i) = RandomSayi(0, 80)
For k = 0 To i - 1
If (Geçici(i) <> Geçici(k)) Then
Next k
Kalıcı(i) = Geçici(i)
i = i + 1
End If
enddo
End Sub
Re: Compile Error: Next without For
Quote:
Originally Posted by
Hijazzer
im having same error but why ?
Code:
Dim Geçici(28),Kalıcı(28) as integer
Public Sub RandomIndis()
Dim i, k As Integer
Geçici(0) = RandomSayi(0, 80)
Kalıcı(0) = Geçici(0)
i = 1
While (i < 28)
Geçici(i) = RandomSayi(0, 80)
For k = 0 To i - 1
If (Geçici(i) <> Geçici(k)) Then
Next k
Kalıcı(i) = Geçici(i)
i = i + 1
End If
Wend
End Sub
hehe tried a couple of loop combinations thats why i missed that enddo statement :D
Re: Compile Error: Next without For
Either your 'Next k' or 'End If' are in the wrong place
Re: Compile Error: Next without For
im trying to pick not duplicated 29 random number in range 0-80. For this i define a function to pick a random number in definable range.
Code:
Function RandomSayi(ByVal enküçük As Integer, ByVal enbüyük As Integer) As Integer
RandomSayi = Int((enbüyük - enküçük + 1) * Rnd + enküçük)
End Function
after that i define global 2 arrays which range is 29 named Geçici(28) and Kalıcı(28)
Code:
Dim Kalıcı(28), Geçici(28) as integer
and than i wanted a public Sub for Creating a Kalıcı(28) array whose members are random and not duplicated integers
Code:
Public Sub RandomIndis()
Dim i, k As Integer
Geçici(0) = RandomSayi(0, 80)
Kalıcı(0) = Geçici(0)
i = 1
While (i < 28)
Geçici(i) = RandomSayi(0, 80)
For k = 0 To i - 1 Step 1
If (Geçici(i) <> Geçici(k)) Then
Next k
Kalıcı(i) = Geçici(i)
lbIndis.AddItem (Str(Kalıcı(i)))
i = i + 1
End If
Wend
in this algorythm i want to pick the Geçici(i) and check ifnot there is any duplicated members in same array . and if it cant find any duplicates it will be my Kalıcı(i) array's i. member ; To do this i need to put
next k code
the exact place that already it placed..
Re: Compile Error: Next without For
i changed my algorythm with a boolean type controller but still it adds some dublicated numbers.
Code:
Public Sub RandomIndis()
Dim i, k As Integer
Dim kontrol As Boolean
Geçici(0) = RandomSayi(0, 80)
Kalıcı(0) = Geçici(0)
i = 1
kontrol = True
While (i < 28)
Geçici(i) = RandomSayi(0, 80)
For k = 0 To i - 1 Step 1
If (Geçici(i) = Geçici(k)) Then
kontrol = False
Else
kontrol = True
End If
Next k
If (kontrol = True) Then
Kalıcı(i) = Geçici(i)
lbIndis.AddItem (Str(Kalıcı(i)))
i = i + 1
Else
i = i - 1
End If
Wend
lbIndis.AddItem (Str(i))
End Sub
Re: Compile Error: Next without For
close... after setting your boolean when you find the duplicate, you need to exit the for loop... otherwise when it checks the next number, it DOESN'T match and it re-sets your boolean.
The alternative is to set the boolean to FALSE before you start you loop AND ONLY change it when you find a match.
or both... which is probably how I'd do it given the code you have.
Code:
Dim FoundDuplicate as boolean
While (i < 28)
Geçici(i) = RandomSayi(0, 80)
FoundDuplicate = False 'assume no duplicate
For k = 0 To i - 1 Step 1
If (Geçici(i) = Geçici(k)) Then
FoundDuplicate = True ' set a flag showing we found a duplicate
exit For ' this exits the for loop... since we found a duplicate, there's no point in checking the rest.
End If
Next k
If not FoundDuplicate Then 'if no duplicate was found
Kalıcı(i) = Geçici(i)
lbIndis.AddItem (Str(Kalıcı(i)))
i = i + 1 ' move on to the next one
End If
Wend
\-tg
Re: Compile Error: Next without For
God bless you friend. thanks alot.. :) i was close but not enough :)
Re: Compile Error: Next without For
Am I correct in thinking you want 29 random numbers from the range 0-80?
her to suggest another easier way to do this, if this is what you are doing.
Re: Compile Error: Next without For
Nope i want 29 numbers between 0-80 range without any duplicates.:)