Link to home
Start Free TrialLog in
Avatar of Juan Velasquez
Juan VelasquezFlag for United States of America

asked on

How to simulate a continue statment in vba for loop

I am trying to go to the next iteration of a for loop when certain conditions are met.  In this case when the column number is 18 and the value is 0.  The code in question is on line 21 where I go back to NextLoop:  Does my code look right.  Thanks.
Public Function RangeToCSV(list As Range) As String
    ' Comments:
    ' Params  : list
    ' Returns : String
    ' Modified:
    
    'TVCodeTools ErrorEnablerStart
    On Error GoTo PROC_ERR
    'TVCodeTools ErrorEnablerEnd
    
    Dim strTmp As String
    Dim lngCurrentRow As Long
    Dim lngCurrentColumn As Long
    Dim rng As Range
    Dim xlsRange As Range
    
    If TypeName(list) = "Range" Then
        lngCurrentRow = 15
NextLoop:
        For Each rng In list.Cells
            If Range(rng.Address).Column > 14 Then
                If Range(rng.Address).Column = 18 Then
                    If rng.value = 0 Or rng.value = "" Then
                        GoTo NextLoop
                    End If
                End If
                    
                If rng.row = lngCurrentRow Then
                    If strTmp = vbNullString Then
                        strTmp = rng.value
                    Else
                        strTmp = strTmp & "," & rng.value
                    End If
                Else
                    lngCurrentRow = lngCurrentRow + 1
                    If strTmp = vbNullString Then
                        strTmp = rng.value
                    Else
                        strTmp = strTmp & Chr(10) & rng.value
                    End If
                End If
            End If
        Next
    End If
    
    RangeToCSV = strTmp
    
    'TVCodeTools ErrorHandlerStart
PROC_EXIT:
    Application.EnableEvents = True
    Exit Function
    
PROC_ERR:
    MsgBox Err.Description, vbCritical, "Sheet1.RangeToCSV"
    Resume PROC_EXIT
    'TVCodeTools ErrorHandlerEnd
    
End Function

Open in new window

ASKER CERTIFIED SOLUTION
Avatar of junholee
junholee
Flag of Korea, Republic of image

Link to home
membership
This solution is only available to members.
To access this solution, you must be a member of Experts Exchange.
Start Free Trial