PDA

Click to See Complete Forum and Search --> : Exiting a Loop [Resolved]


Pearso
Jun 8th, 2005, 06:50 PM
Hello all,

I have a loop which i want to skip to the next i if a condition is meet how do i do this. My code is.

For i = 2 To 16

x = i + 1
EmployeeName = Sheets("Summary").Cells(i, 1)
EmployeeNum = Sheets("Summary").Cells(i, 2)
EmployeeNumEnd = Sheets("Summary").Cells(x, 2)
Sheets(EmployeeName).Select
Columns("A:J").Select
Selection.ClearContents
If EmployeeNum = 0 Then
Next i
End If
If EmployeeNumEnd = 0 Then
x = i + 2
EmployeeNumEnd = Sheets("Summary").Cells(x, 2)
End If
ActiveWorkbook.Sheets("Import").Select
y = EmployeeNumEnd - 1
Rows(EmployeeNum & ":" & y).Select
Selection.Cut
Sheets(EmployeeName).Select
Range("A3").Select
ActiveSheet.Paste
Range("A1") = DateReport
Range("A2:J2") = Heading
Range("A1").Select
Next i

The problem i'm having is in the first IF statment is says that the Next i is not matched with and for statment. How can do what i'm trying to achieve.

Any help would be greatly appreicated.

Regards,

Pearso :confused:

salvelinus
Jun 8th, 2005, 09:07 PM
I may be getting my languages mixed up here, but look up Break & Continue.

Pearso
Jun 8th, 2005, 09:26 PM
Thanks for the suggestion but i had no luck find anything that would help.

If anyone else has any ideas it would be greatly appreicated.

RobDog888
Jun 8th, 2005, 10:59 PM
You can do it like this...
For i = 2 To 16

x = i + 1
EmployeeName = Sheets("Summary").Cells(i, 1)
EmployeeNum = Sheets("Summary").Cells(i, 2)
EmployeeNumEnd = Sheets("Summary").Cells(x, 2)
Sheets(EmployeeName).Select
Columns("A:J").Select
Selection.ClearContents
If EmployeeNum = 0 Then
'Next i
Else
If EmployeeNumEnd = 0 Then
x = i + 2
EmployeeNumEnd = Sheets("Summary").Cells(x, 2)
End If
ActiveWorkbook.Sheets("Import").Select
y = EmployeeNumEnd - 1
Rows(EmployeeNum & ":" & y).Select
Selection.Cut
Sheets(EmployeeName).Select
Range("A3").Select
ActiveSheet.Paste
Range("A1") = DateReport
Range("A2:J2") = Heading
Range("A1").Select
End If

Next i

Pearso
Jun 9th, 2005, 06:14 PM
The solution was that easy,

Thanks for the help..