-
May 5th, 2005, 08:25 AM
#1
Thread Starter
New Member
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
-
May 5th, 2005, 08:42 AM
#2
Re: Compile Error: Next without For
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
Feeling like a fly on the inside of a closed window (Thunk!)
If I post a lot, it is because I am bored at work! ;D Or stuck...
* Anything I post can be only my opinion. Advice etc is up to you to persue...
-
May 5th, 2005, 11:52 AM
#3
Thread Starter
New Member
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
-
Aug 19th, 2009, 02:27 PM
#4
New Member
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
-
Aug 19th, 2009, 02:45 PM
#5
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...
A good exercise for the Heart is to bend down and help another up...
Please Mark your Thread "Resolved", if the query is solved
MyGear:
★ CPU ★ Ryzen 5 5800X
★ GPU ★ NVIDIA GeForce RTX 3080 TI Founder Edition
★ RAM ★ G. Skill Trident Z RGB 32GB 3600MHz
★ MB ★ ASUS TUF GAMING X570 (WI-FI) ATX Gaming
★ Storage ★ SSD SB-ROCKET-1TB + SEAGATE 2TB Barracuda IHD
★ Cooling ★ NOCTUA NH-D15 CHROMAX BLACK 140mm + 10 of Noctua NF-F12 PWM
★ PSU ★ ANTEC HCG-1000-EXTREME 1000 Watt 80 Plus Gold Fully Modular PSU
★ Case ★ LIAN LI PC-O11 DYNAMIC XL ROG (BLACK) (G99.O11DXL-X)
★ Monitor ★ LG Ultragear 27" 240Hz Gaming Monitor
★ Keyboard ★ TVS Electronics Gold Keyboard
★ Mouse ★ Logitech G502 Hero
-
Aug 19th, 2009, 02:46 PM
#6
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
-
Aug 19th, 2009, 02:51 PM
#7
New Member
Re: Compile Error: Next without For
-
Mar 2nd, 2012, 11:19 AM
#8
New Member
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
-
Mar 2nd, 2012, 11:22 AM
#9
New Member
Re: Compile Error: Next without For
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
-
Mar 2nd, 2012, 11:23 AM
#10
Addicted Member
Re: Compile Error: Next without For
Either your 'Next k' or 'End If' are in the wrong place
-
Mar 2nd, 2012, 11:47 AM
#11
New Member
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..
-
Mar 2nd, 2012, 12:14 PM
#12
New Member
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
-
Mar 2nd, 2012, 12:43 PM
#13
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
-
Mar 2nd, 2012, 12:59 PM
#14
New Member
Re: Compile Error: Next without For
God bless you friend. thanks alot.. i was close but not enough
-
Mar 2nd, 2012, 05:49 PM
#15
Frenzied Member
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.
-
Mar 2nd, 2012, 07:35 PM
#16
New Member
Re: Compile Error: Next without For
Nope i want 29 numbers between 0-80 range without any duplicates.
Posting Permissions
- You may not post new threads
- You may not post replies
- You may not post attachments
- You may not edit your posts
-
Forum Rules
|
Click Here to Expand Forum to Full Width
|