Compile Error: Next without For-VBForums
Results 1 to 16 of 16

Thread: Compile Error: Next without For

  1. #1

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    6

    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

  2. #2
    Don't Panic! Ecniv's Avatar
    Join Date
    Nov 2000
    Location
    Excel Hell!
    Posts
    4,973

    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

    BOFH Now, BOFH Past, Information on duplicates

    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...

  3. #3

    Thread Starter
    New Member
    Join Date
    May 2005
    Posts
    6

    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

  4. #4
    New Member
    Join Date
    Aug 2009
    Location
    ALABAMA
    Posts
    13

    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

  5. #5
    Super Moderator Siddharth Rout's Avatar
    Join Date
    Feb 2005
    Location
    Mumbai, India
    Posts
    11,928

    Re: Compile Error: Next without For

    The error is because you are missing an End If. See below...

    vb Code:
    1. While Not records.EOF
    2.     For i = 0 To 6
    3.         If i = 5 Then
    4.             i = 8
    5.         ElseIf i = 6 Then
    6.             i = 11
    7.         End If '<~~~ YOU ARE MISSING THIS
    8.  
    9.         If isPlanLocked(records!Year, i) Then
    10.             Forms!frmMain!frameDivision.Value = i
    11.             DoCmd.OpenForm "frmPlan"
    12.             Forms!frmPlan!txtYear.Value = records!Year
    13.             DoCmd.OpenForm "frmPrintPlan"
    14.             PrintPlan
    15.             DoCmd.OutputTo acOutputReport, "rptPlan", _
    16.             "Snapshot Format", Path & Forms!frmPlan!txtYear.Value & _
    17.             " " & FindDivisionName(Forms!frmMain!frameDivision.Value) & ".snp"
    18.             DoCmd.Close acReport, "rptPlan"
    19.             DoCmd.Close acForm, "frmPrintPlan"
    20.             DoCmd.Close acForm, "frmPlan"
    21.         End If
    22.  
    23.         records.MoveNext
    24.     Next i
    25. Wend
    26. 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...
    The poster formerly known as koolsid
    A good exercise for the Heart is to bend down and help another up...
    Please Mark your Thread "Resolved", if the query is solved

    Microsoft MVP: 2011 - Till Date IMP Links : Acceptable Use Policy, FAQ

    MyGear:
    Sony VGN-FZ27G with a triple boot between (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008) and (Win7+Office 2010+VS2010) || Sony VPCCB-45FN with a Win7+Office 2010+VS2010. VM: (XP+Office 2003+VB6), (VISTA+Office 2007+VS2008), (Win8+Office 2010+VS2012) || Mac Book Pro (10.6.8) with Office 2011

  6. #6
    PowerPoster
    Join Date
    May 2002
    Posts
    25,298

    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
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  7. #7
    New Member
    Join Date
    Aug 2009
    Location
    ALABAMA
    Posts
    13

    Re: Compile Error: Next without For

    Thanks guys...

  8. #8
    New Member
    Join Date
    Mar 2012
    Posts
    13

    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

  9. #9
    New Member
    Join Date
    Mar 2012
    Posts
    13

    Re: Compile Error: Next without For

    Quote Originally Posted by Hijazzer View Post
    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

  10. #10
    Addicted Member
    Join Date
    Jan 2009
    Posts
    182

    Re: Compile Error: Next without For

    Either your 'Next k' or 'End If' are in the wrong place

  11. #11
    New Member
    Join Date
    Mar 2012
    Posts
    13

    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&#252;&#231;&#252;k As Integer, ByVal enb&#252;y&#252;k As Integer) As Integer
    RandomSayi = Int((enb&#252;y&#252;k - enk&#252;&#231;&#252;k + 1) * Rnd + enk&#252;&#231;&#252;k)
    End Function
    after that i define global 2 arrays which range is 29 named Ge&#231;ici(28) and Kalıcı(28)
    Code:
    Dim Kalıcı(28), Ge&#231;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&#231;ici(0) = RandomSayi(0, 80)
    Kalıcı(0) = Ge&#231;ici(0)
    i = 1
    While (i < 28)
    Ge&#231;ici(i) = RandomSayi(0, 80)
    
        For k = 0 To i - 1 Step 1
           If (Ge&#231;ici(i) <> Ge&#231;ici(k)) Then
        Next k
         Kalıcı(i) = Ge&#231;ici(i)
            lbIndis.AddItem (Str(Kalıcı(i)))
           i = i + 1
        End If
       
    Wend
    in this algorythm i want to pick the Ge&#231;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..

  12. #12
    New Member
    Join Date
    Mar 2012
    Posts
    13

    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&#231;ici(0) = RandomSayi(0, 80)
    Kalıcı(0) = Ge&#231;ici(0)
    i = 1
    kontrol = True
    While (i < 28)
    Ge&#231;ici(i) = RandomSayi(0, 80)
    
        For k = 0 To i - 1 Step 1
           If (Ge&#231;ici(i) = Ge&#231;ici(k)) Then
         kontrol = False
         Else
         kontrol = True
         End If
         Next k
         If (kontrol = True) Then
         Kalıcı(i) = Ge&#231;ici(i)
            lbIndis.AddItem (Str(Kalıcı(i)))
           i = i + 1
           Else
          i = i - 1
        End If
       
    Wend
    lbIndis.AddItem (Str(i))
    
    End Sub

  13. #13
    PowerPoster
    Join Date
    May 2002
    Posts
    25,298

    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&#231;ici(i) = RandomSayi(0, 80)
      FoundDuplicate = False 'assume no duplicate
        For k = 0 To i - 1 Step 1
           If (Ge&#231;ici(i) = Ge&#231;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&#231;ici(i)
            lbIndis.AddItem (Str(Kalıcı(i)))
            i = i + 1 ' move on to the next one
        End If
       
    Wend
    \-tg
    * I don't respond to private (PM) requests for help. It's not conducive to the general learning of others.*
    * I also don't respond to friend requests. Save a few bits and don't bother. I'll just end up rejecting anyways.*
    * How to get EFFECTIVE help: The Hitchhiker's Guide to Getting Help at VBF - Removing eels from your hovercraft *
    * How to Use Parameters * Create Disconnected ADO Recordset Clones * Set your VB6 ActiveX Compatibility * Get rid of those pesky VB Line Numbers * I swear I saved my data, where'd it run off to??? *

  14. #14
    New Member
    Join Date
    Mar 2012
    Posts
    13

    Re: Compile Error: Next without For

    God bless you friend. thanks alot.. i was close but not enough

  15. #15
    Frenzied Member
    Join Date
    Nov 2010
    Posts
    1,197

    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.

  16. #16
    New Member
    Join Date
    Mar 2012
    Posts
    13

    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
  •  



Featured


Click Here to Expand Forum to Full Width

Survey posted by VBForums.