Next without For ERROR

avd88

Board Regular
Joined
Jan 18, 2016
Messages
112
I'm getting the error Next without For in my following code can't figure out why:

I'm just copying the section of the code with the For loop. I'm using GoTo so that whenever a section of the code that's true is ran it will just go to the next i instead of running the subsequent sections that wouldn't apply once a section is true.

VBA Code:
Dim lastrow As Long
 Dim i As Integer
 
 Dim AttainmentDysp As Double
 Dim AttainmentFill As Double
 Dim AttainmentTotInj As Double
 Dim DyspMileOneAttainment As Double
 Dim DyspMileTwoAttainment As Double
 Dim FillMileOneAttainment As Double
 Dim FillMileTwoAttainment As Double

 
 lastrow = Sheets("Attainment").Cells(Rows.Count, 1).End(xlUp).Row
 
 For i = 4 To lastrow
 
 AttainmentDysp = Application.WorksheetFunction.Round(Sheets("Attainment").Cells(i, 6).Value, 3)
 AttainmentFill = Application.WorksheetFunction.Round(Sheets("Attainment").Cells(i, 18).Value, 3)
 AttainmentTotInj = Application.WorksheetFunction.Round(Sheets("Attainment").Cells(i, 30).Value, 3)
 DyspMileOneAttainment = Application.WorksheetFunction.Round(Sheets("Attainment").Cells(i, 9).Value, 3)
 DyspMileTwoAttainment = Application.WorksheetFunction.Round(Sheets("Attainment").Cells(i, 11).Value, 3)
 FillMileOneAttainment = Application.WorksheetFunction.Round(Sheets("Attainment").Cells(i, 21).Value, 3)
 FillMileTwoAttainment = Application.WorksheetFunction.Round(Sheets("Attainment").Cells(i, 23).Value, 3)


    
    'MINIMUM 95% REQUIREMENT ON BOTH CATEGORIES CHECK
    If AttainmentDysp + DyspContestMod < DyspQualMinReq Or AttainmentFill + FillContestMod < FillQualMinReq Then
        Sheets("Attainment").Cells(i, 12).Value = 0
        Sheets("Attainment").Cells(i, 24).Value = 0
        Sheets("Attainment").Cells(i, 32).Value = "Y"

        GoTo Last
    End If
    
    'IF 3 OR MORE MILESTONES MISSED CHECK
      
    If MileCounter(i) <= 1 Then
        Sheets("Attainment").Cells(i, 12).Value = 0
        Sheets("Attainment").Cells(i, 24).Value = 0
        Sheets("Attainment").Cells(i, 32).Value = "Y"
        'Sheets("Attainment").Cells(i, 35).Value = MileCounter(i) FOR CHECK DELETE WHEN DONE

        GoTo Last
    End If
    
    'BOTH CATEGORIES BELOW 100% CHECK
    If AttainmentDysp + DyspContestMod < DyspQualReq And AttainmentFill + FillContestMod < FillQualReq Then
        Sheets("Attainment").Cells(i, 12).Value = 0
        Sheets("Attainment").Cells(i, 24).Value = 0
        Sheets("Attainment").Cells(i, 32).Value = "Y"

        
        GoTo Last
    End If
    
'MULL CHECK'
    Dim MileCount As Integer
    MileCount = MileCounter(i)
    
    If MileCount = 2 Then
        TotInjMullAttain = Sheets("Standard Inputs").Range("H12").Value
    ElseIf MileCount = 3 Then
        TotInjMullAttain = Sheets("Standard Inputs").Range("I12").Value
    ElseIf MileCount = 4 Then
        TotInjMullAttain = Sheets("Standard Inputs").Range("J12").Value

    If (MileCount < 4 Or _
        (AttainmentDysp + DyspContestMod >= DyspMullAttainMin And AttainmentDysp + DyspContestMod < DyspQualReq) Or _
        (AttainmentFill + FillContestMod >= FillMullAttainMin And AttainmentFill + FillContestMod < FillQualReq)) And _
        MileCount >= 2 Then
        
        If (AttainmentDysp + DyspContestMod >= DyspMullAttainMin And AttainmentFill + FillContestMod >= FillMullAttainMin) And _
            AttainmentTotInj >= TotInjMullAttain Then
            
            Sheets("Attainment").Cells(i, 12).Value = DyspPayout + Dysp2Payout
            Sheets("Attainment").Cells(i, 24).Value = FillPayout + Fill2Payout
            Sheets("Attainment").Cells(i, 32).Value = "Y"
            Sheets("Attainment").Cells(i, 33).Value = "Y"
            
            
            'BONUSES------------------------------------------------------------------------------------------------------------------------------------------------'
        'When inputs at 0
            If DyspBonusPayout = 0 And FillBonusPayout = 0 And DyspBonus2Payout = 0 And FillBonus2Payout = 0 Then
                GoTo Last
            End If
            
            Dim TotalDyspBonus As Integer
            Dim TotalFillBonus As Integer
            
            TotalDyspBonus = DyspPayout + Dysp2Payout
            TotalFillBonus = FillPayout + Fill2Payout
            
            'D Bonuses
            If DyspMileOneAttainment >= DyspBonusReq Then
                TotalDyspBonus = TotalDyspBonus + DyspBonusPayout
                Sheets("Attainment").Cells(i, 12).Value = TotalDyspBonus
            End If
            
            If DyspMileTwoAttainment >= DyspBonus2Req Then
                TotalDyspBonus = TotalDyspBonus + DyspBonus2Payout
                Sheets("Attainment").Cells(i, 12).Value = TotalDyspBonus
            End If
            
            'TF Bonuses
            If FillMileOneAttainment >= FillBonusReq Then
                TotalFillBonus = TotalFillBonus + FillBonusPayout
                Sheets("Attainment").Cells(i, 24).Value = TotalFillBonus
            End If
            
            If FillMileTwoAttainment >= FillBonus2Req Then
                TotalFillBonus = TotalFillBonus + FillBonus2Payout
                Sheets("Attainment").Cells(i, 24).Value = TotalFillBonus
            End If
            
                
            GoTo Last
        End If
    
    End If
    
    
'ACC CHECKS
    MileCount = MileCounter(i)
    'Four Hits
    If MileCount = 4 Then
        Call FourHits(i)
        
    ElseIf MileCount = 3 Then
        Call ThreeHits(i)
        
    ElseIf MileCount = 2 Then
        Call TwoHits(i)
    
    End If
    
Last:
 Next i
 

Excel Facts

Can Excel fill bagel flavors?
You can teach Excel a new custom list. Type the list in cells, File, Options, Advanced, Edit Custom Lists, Import, OK
I think here you are missing an End IF:

Rich (BB code):
    If MileCount = 2 Then
        TotInjMullAttain = Sheets("Standard Inputs").Range("H12").Value
    ElseIf MileCount = 3 Then
        TotInjMullAttain = Sheets("Standard Inputs").Range("I12").Value
    ElseIf MileCount = 4 Then
        TotInjMullAttain = Sheets("Standard Inputs").Range("J12").Value
    End If

    If (MileCount < 4 Or _

Note: It is not advisable to use GoTo instruction. You must organize your code to avoid using GoTo.

If the code does not work, put here a sample of your data, use XL2BB tool minisheet and in another minisheet the result you expect and I help you simplify your macro.
 
Upvote 0

Forum statistics

Threads
1,215,059
Messages
6,122,913
Members
449,093
Latest member
dbomb1414

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.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

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

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

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
Back
Top