Results 1 to 9 of 9

Thread: [RESOLVED] How to force Next before end?

  1. #1

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    7

    Resolved [RESOLVED] How to force Next before end?

    In BASIC I could write something like

    For x = 1 to 3
    If x=2 then next x (or goto line number of above code)
    print x+" is an odd number."
    next x

    vba does not seem to recognise the "next x" when in the if nest though.


    How do I get around this? I can do x=x+1 because some of the exceptions are consecutive and will not be caught when together (unlike my even number example).

    Thanks.

  2. #2
    Addicted Member
    Join Date
    Jan 2009
    Posts
    233

    Re: How to force Next before end?

    hi palantir, don't know exactly what you want to do but hope this is what you mean:

    one more thing don't put goto line number which is above your "for loop" because it will be an eternal loop ...

    Code:
    For x = 1 to 3
    
    If x = 2 then
    
    Goto line_u_want '(goto line_u_want should be below for loop)
    
    Exit For
    
    End if
    
    Next x
    The taller the bamboo grows the lower it bends...

  3. #3
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to force Next before end?

    vb Code:
    1. For x = 1 to 3
    2.   If x=2 then ' do nothing         next x (or goto line number of above code)
    3.   else
    4.      print x & " is an odd number."
    5.   end if
    6. next x
    or
    vb Code:
    1. For x = 1 to 3
    2.   If Not x=2 then print x &" is an odd number."
    3. next x

    note indenting to make code readable
    use & rather than + to concantenate strings, + may work, but may give incorrect results sometimes
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  4. #4
    Head Hunted anhn's Avatar
    Join Date
    Aug 2007
    Location
    Australia
    Posts
    3,669

    Re: How to force Next before end?

    These simulate what you want. However, for better practice, if possible you should try to avoid using GoTo. Use the way westconn1 mentioned above.
    Code:
    Sub XXX()
        Dim x As Long
    
        For x = 1 To 10
            If x Mod 2 = 0 Then GoTo Next_x '-- use Goto Label
            '... ...
            Debug.Print x & " is an odd number."
    Next_x:
        Next
    
    End Sub
    
    Sub XYZ()
        Dim x As Long
    
        For x = 1 To 10
            If x Mod 2 = 0 Then GoTo 99 '-- use Goto Line number
            '... ...
            Debug.Print x & " is an odd number."
    99  Next
    
    End Sub
    • Don't forget to use [CODE]your code here[/CODE] when posting code
    • If your question was answered please use Thread Tools to mark your thread [RESOLVED]
    • Don't forget to RATE helpful posts

    • Baby Steps a guided tour
    • IsDigits() and IsNumber() functions • Wichmann-Hill Random() function • >> and << functions for VB • CopyFileByChunk

  5. #5

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    7

    Re: How to force Next before end?

    Thank you anhn and all. Resolved. It was the GoTo Label I needed.

  6. #6
    PowerPoster
    Join Date
    Dec 2004
    Posts
    25,618

    Re: How to force Next before end?

    it is not considered to be good to use goto or gosub in VB, except for error handling
    i do my best to test code works before i post it, but sometimes am unable to do so for some reason, and usually say so if this is the case.
    Note code snippets posted are just that and do not include error handling that is required in real world applications, but avoid On Error Resume Next

    dim all variables as required as often i have done so elsewhere in my code but only posted the relevant part

    come back and mark your original post as resolved if your problem is fixed
    pete

  7. #7

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    7

    Re: How to force Next before end?

    So how else can it be done?

    Here's my final working and tested code (excerpt:

    Code:
    Sub EDUmation()
    ...
    For NumEntry = 1 To Range("B4").Value
    FirstRow = ((NumEntry * 33) - 33) + 10 ' gets first row of each edu entry per loop
    LastRow = FirstRow + 31 ' adds 31 rows to include all of entry
     For MyRow = FirstRow To LastRow ' loop through each row of the table
      If Cells(MyRow, 2).Value = "" Then GoTo Next_MyRow ' Gets next row if current row has no items
     mystr = ""
     mystr = Cells(MyRow, 1).Value & String(17 - Len(Cells(MyRow, 1).Value), " ") ' element name + horizontal spacer
     mystr = mystr & Cells(MyRow, 2).Value ' adds item in row 1
        For CheckRow = 3 To 31 ' this loop adds any other items except where empty
         If Not Cells(MyRow, CheckRow).Value = "" Then mystr = mystr & ", " & Cells(MyRow, CheckRow).Value
        Next CheckRow
     Print #1, mystr
    Next_MyRow:
     Next MyRow
     Print #1, vbCrLf
    Next NumEntry
    Close #1
    Sheets("DATA").Range("G2").ClearContents ' note that this row expects the worksheet tab to be named DATA
    Sheets("DATA").Hyperlinks.Add Range("G2"), PageName
    End Sub

  8. #8
    I'm about to be a PowerPoster! Hack's Avatar
    Join Date
    Aug 2001
    Location
    Searching for mendhak
    Posts
    58,333

    Re: How to force Next before end?

    Quote Originally Posted by palantir View Post
    So how else can it be done?
    How about
    Code:
    For NumEntry = 1 To Range("B4").Value
        FirstRow = ((NumEntry * 33) - 33) + 10 ' gets first row of each edu entry per loop
        LastRow = FirstRow + 31 ' adds 31 rows to include all of entry
         For MyRow = FirstRow To LastRow ' loop through each row of the table
             If Cells(MyRow, 2).Value <> "" Then
                mystr = ""
                mystr = Cells(MyRow, 1).Value & String(17 - Len(Cells(MyRow, 1).Value), " ") ' element name + horizontal spacer
                mystr = mystr & Cells(MyRow, 2).Value ' adds item in row 1
                   For CheckRow = 3 To 31 ' this loop adds any other items except where empty
                        If Not Cells(MyRow, CheckRow).Value = "" Then mystr = mystr & ", " & Cells(MyRow, CheckRow).Value
                   Next 'CheckRow
                Print #1, mystr
             End If
         Next 'MyRow
            Print #1, vbCrLf
    Next 'NumEntry
    Close #1

  9. #9

    Thread Starter
    New Member
    Join Date
    Apr 2009
    Posts
    7

    Re: How to force Next before end?

    You're a genius. Thank you so much! Resolved.

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