Results 1 to 5 of 5

Thread: Exiting a Loop [Resolved]

  1. #1

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    46

    Resolved Exiting a Loop [Resolved]

    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.

    VB Code:
    1. For i = 2 To 16
    2.  
    3.         x = i + 1
    4.         EmployeeName = Sheets("Summary").Cells(i, 1)
    5.         EmployeeNum = Sheets("Summary").Cells(i, 2)
    6.         EmployeeNumEnd = Sheets("Summary").Cells(x, 2)
    7.         Sheets(EmployeeName).Select
    8.         Columns("A:J").Select
    9.         Selection.ClearContents
    10.         If EmployeeNum = 0 Then
    11.             Next i
    12.         End If
    13.         If EmployeeNumEnd = 0 Then
    14.             x = i + 2
    15.             EmployeeNumEnd = Sheets("Summary").Cells(x, 2)
    16.         End If
    17.         ActiveWorkbook.Sheets("Import").Select
    18.         y = EmployeeNumEnd - 1
    19.         Rows(EmployeeNum & ":" & y).Select
    20.         Selection.Cut
    21.         Sheets(EmployeeName).Select
    22.         Range("A3").Select
    23.         ActiveSheet.Paste
    24.         Range("A1") = DateReport
    25.         Range("A2:J2") = Heading
    26.         Range("A1").Select
    27.     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
    Last edited by Pearso; Jun 9th, 2005 at 06:15 PM.

  2. #2
    Frenzied Member
    Join Date
    Feb 2003
    Location
    Argentina
    Posts
    1,950

    Re: Exiting a Loop

    I may be getting my languages mixed up here, but look up Break & Continue.
    Tengo mas preguntas que contestas

  3. #3

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    46

    Re: Exiting a Loop

    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.

  4. #4
    Ex-Super Mod RobDog888's Avatar
    Join Date
    Apr 2001
    Location
    LA, Calif. Raiders #1 AKA:Gangsta Yoda™
    Posts
    60,709

    Re: Exiting a Loop

    You can do it like this...
    VB Code:
    1. For i = 2 To 16
    2.  
    3.     x = i + 1
    4.     EmployeeName = Sheets("Summary").Cells(i, 1)
    5.     EmployeeNum = Sheets("Summary").Cells(i, 2)
    6.     EmployeeNumEnd = Sheets("Summary").Cells(x, 2)
    7.     Sheets(EmployeeName).Select
    8.     Columns("A:J").Select
    9.     Selection.ClearContents
    10.     If EmployeeNum = 0 Then
    11.         'Next i
    12.     Else
    13.         If EmployeeNumEnd = 0 Then
    14.             x = i + 2
    15.             EmployeeNumEnd = Sheets("Summary").Cells(x, 2)
    16.         End If
    17.         ActiveWorkbook.Sheets("Import").Select
    18.         y = EmployeeNumEnd - 1
    19.         Rows(EmployeeNum & ":" & y).Select
    20.         Selection.Cut
    21.         Sheets(EmployeeName).Select
    22.         Range("A3").Select
    23.         ActiveSheet.Paste
    24.         Range("A1") = DateReport
    25.         Range("A2:J2") = Heading
    26.         Range("A1").Select
    27.     End If
    28.        
    29. Next i
    VB/Office Guru™ (AKA: Gangsta Yoda®)
    I dont answer coding questions via PM. Please post a thread in the appropriate forum.

    Microsoft MVP 2006-2011
    Office Development FAQ (C#, VB.NET, VB 6, VBA)
    Senior Jedi Software Engineer MCP (VB 6 & .NET), BSEE, CET
    If a post has helped you then Please Rate it!
    Reps & Rating PostsVS.NET on Vista Multiple .NET Framework Versions Office Primary Interop AssembliesVB/Office Guru™ Word SpellChecker™.NETVB/Office Guru™ Word SpellChecker™ VB6VB.NET Attributes Ex.Outlook Global Address ListAPI Viewer utility.NET API Viewer Utility
    System: Intel i7 6850K, Geforce GTX1060, Samsung M.2 1 TB & SATA 500 GB, 32 GBs DDR4 3300 Quad Channel RAM, 2 Viewsonic 24" LCDs, Windows 10, Office 2016, VS 2019, VB6 SP6

  5. #5

    Thread Starter
    Member
    Join Date
    Mar 2005
    Location
    Australia
    Posts
    46

    Re: Exiting a Loop

    The solution was that easy,

    Thanks for the help..

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  



Click Here to Expand Forum to Full Width