freckles9287
freckles9287

Reputation: 15

How to remedy Next without For VBA complier error?

I have a similar VBA that does a similar function that runs without issues. However, when I try to run my new program, I get the 'Next without For' compile error.

Below is the code that runs without issues.

Sub FillIn_Curing_Container()

Set wsL = Worksheets("Storage Label")
Set ws006 = Worksheets("F 006 Container Pre-Inspection")
Set ws010 = Worksheets("F 010 RETIRED SHEET")
Set ws013 = Worksheets("F 013 Mold Assembly Check List")

Call Fix_Design_Codes_and_SILR

If TypeName(Selection) = "Range" Then
    For Each i In Selection.Cells
    jobNumber = i.Value
    
        With ws006
            .Range("A1").Value = jobNumber
            .Range("A4").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:E, 2, FALSE)"
            .Range("A5").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:E, 4, FALSE)"
            .Range("M5").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:E, 3, FALSE)"
        End With
        
        With ws013
            .Range("A1").Value = jobNumber
            .Range("A3").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:E, 2, FALSE)"
            .Range("A4").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:E, 4, FALSE)"
            .Range("C4").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:E, 3, FALSE)"
        End With
        
        With ws010
            .Range("AJ1").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:E, 5, FALSE)"
        End With
        
'        With wsL
'            .Range("A1").Value = jobNumber
'            .Range("A2").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:E, 4, FALSE)"
'            .Range("A3").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:E, 2, FALSE)"
'            .Range("A6").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:J, 10, FALSE)"
'        End With
    
        Call Print_Curing
    Next i
Else
    MsgBox "Select 1 or More Job Number Cells"
End If
    
On Error Resume Next

And below this is the code that flags the compiler error.

Sub Print_Storage_Labels()

Set wsD = Worksheets("Database")
Set wsL = Worksheets("Storage Label")

moldCount = 0

If TypeName(Selection) = "Range" Then
    For Each i In Selection.Cells
        jobNumber = i.Value
    
        row = ActiveCell.row + 1
        secondJobNumber = wsD.Cells(row, 1).Value
    
    If (wsD.Cells(ActiveCell.row, 15) = "MOLD") Then
        moldCount = moldCount + 2
    Else
        moldCount = moldCount + 0
    End If
        
    
    If (wsD.Cells(ActiveCell.row, 17) = "CONTAINER") Then
        moldCount = moldCount + 3
    Else
        moldCount = moldCount + 0
    End If
      
      
    If moldCount = 0 Then
        MsgBox "Select 1 or more valid job numbers"
    
    ElseIf moldCount = 2 Then
        With wsL
            .Range("A1").Value = jobNumber
            .Range("A2").Formula = "MOLD ONLY"
            .Range("A3").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:J, 2, FALSE)"
            .Range("A6").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:J, 10, FALSE)"
        End With
        
    ElseIf moldCount = 3 Then
        With wsL
            .Range("A1").Formula = "CONTAINER ONLY"
            .Range("A2").Value = jobNumber
            .Range("A6").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:J, 3, FALSE)"
        End With
        
    ElseIf moldCount = 5 Then
        With wsL
            .Range("A1").Value = jobNumber
            .Range("A2").Value = secondJobNumber
            .Range("A3").Formula = "=VLOOKUP(""" & jobNumber & """, 'Database'!A:J, 2, FALSE)"
            .Range("A6").Formula = "=VLOOKUP(""" & secondJobNumber & """, 'Database'!A:J, 3, FALSE)"
        End With
        
    Else
        MsgBox "Program Error: Contact Rachel"
    End If
        
    wsL.PrintOut
    Range(wsL.Cells(1, 1), wsL.Cells(6, 1)).ClearContents
    
    If moldCount = 2 Then
        moldCount = 0
        Next i
    ElseIf moldCount = 3 Then
        moldCount = 0
        Next i
    ElseIf moldCount = 5 Then
        moldCount = 0
        Next i
        Next i
    Else
        MsgBox "Program Error"
    End If
         
End If
End Sub

I'm not sure if the rest of my code will allow it to run or not. I have tried various "fixes" and none of them worked. In case it helps, below are the declarations that I made outside the Subs

Option Explicit
Dim jobNumber As String
Dim i As Range
Dim moldStatus As String
Dim wsD As Worksheet
Dim wsL As Worksheet
Dim moldCount As Long
Dim secondJobNumber As String
Dim row As Long
Dim ws006 As Worksheet
Dim ws010 As Worksheet
Dim ws013 As Worksheet

Any help is appreciated. Thanks!

Upvotes: 0

Views: 43

Answers (1)

Tim Williams
Tim Williams

Reputation: 166381

Some suggestions:

Sub Print_Storage_Labels()

    Dim c As Range, rw As Range, wsD As Worksheet, wsL As Worksheet, moldCount As Long
    Dim jobNumber, secondJobNumber
    
    Set wsD = ThisWorkbook.Worksheets("Database")
    Set wsL = ThisWorkbook.Worksheets("Storage Label")
    
    'might also want to add in a check that the user selected
    '   cell(s) in the correct column...
    If TypeName(Selection) <> "Range" Then
        MsgBox "Please first select one or more job numbers", vbExclamation
        Exit Sub
    End If

    For Each c In Selection.Cells
        
        Set rw = c.EntireRow 'get the whole row for this cell
        moldCount = 0
        
        jobNumber = c.Value
        secondJobNumber = rw.Cells(1).Value

        'no need to add zeroes here
        If rw.Cells(15).Value = "MOLD" Then moldCount = moldCount + 2
        If rw.Cells(17).Value = "CONTAINER" Then moldCount = moldCount + 3
    
        If moldCount = 0 Then
            MsgBox "Job number " & jobNumber & " not valid"
        ElseIf moldCount = 2 Then
            With wsL
                .Range("A1").Value = jobNumber
                .Range("A2").Formula = "MOLD ONLY"
                .Range("A3").Formula = "=VLOOKUP(A1, 'Database'!A:J, 2, FALSE)"
                .Range("A6").Formula = "=VLOOKUP(A1, 'Database'!A:J, 10, FALSE)"
            End With
        ElseIf moldCount = 3 Then
            With wsL
                .Range("A1").Formula = "CONTAINER ONLY"
                .Range("A2").Value = jobNumber
                .Range("A6").Formula = "=VLOOKUP(A2, 'Database'!A:J, 3, FALSE)"
            End With
        ElseIf moldCount = 5 Then
            With wsL
                .Range("A1").Value = jobNumber
                .Range("A2").Value = secondJobNumber
                .Range("A3").Formula = "=VLOOKUP(A1, 'Database'!A:J, 2, FALSE)"
                .Range("A6").Formula = "=VLOOKUP(A2, 'Database'!A:J, 3, FALSE)"
            End With
        Else
            MsgBox "Program Error: Contact Rachel"
            Exit For 'exit the loop on first error
        End If
        
        wsL.Calculate 'make sure formulas are calculated
        wsL.PrintOut
        wsL.Cells(1, 1).Resize(6, 1).ClearContents
    
    Next c
End Sub

Upvotes: 1

Related Questions