Ugo Portela Pereira
Ugo Portela Pereira

Reputation: 109

How to solve 'Next without For' error in VBA

I am receiving a next without for error from:

Sub CTLines()
Dim iVal As Integer
Dim ws1 As Worksheet
Dim ws2 As Worksheet
Dim rng As Range
    
Set ws1 = Worksheets("INCIDENTS")
Set ws2 = Worksheets("INCDB")
    
iVal = Application.WorksheetFunction.CountIf(Range("AO5:AO999"), "Yes")
    
Dim i
    
For i = 1 To iVal
    With Sheets("INCDB")
        .Range("5:5").Insert Shift:=x1Down
Next i
End Sub

I've tried changing the variable, the indent, many things and I've not been successful.

All I want to do is to count how many rows contain Yes in the AO column and add as many rows in the INCDB spreadsheet.

Upvotes: 2

Views: 523

Answers (1)

Excel Hero
Excel Hero

Reputation: 14764

Change the code to this, near the bottom:

For i = 1 To iVal
    With Sheets("INCDB")
        .Range("5:5").Insert Shift:=xlDown
    End With
Next i

The VBA compiler is not good at reporting what is wrong, when it encounters code that has one or more lines that are missing a matching termination line.

In your case you never terminated the With statement.

Upvotes: 5

Related Questions