Hi there,
I wrote a VBA program as below, but I'm not able to exit the sub nor exit do looping if the condition is fulfilled, why so???? It runs through the lines until end subsequently creating empty text file. I want like if the if condition is fulfilled then stop execution and no text file creation. How to accomplish this?
Anyone out there please help me.

Sub EXP_REPORT_TO_TEXT()
           '
           '
           '

    iEnd = 0
    iRow = 1
    
    Do While iEnd = 0
    
    If IsEmpty(ws.Cells(iRow + 1, 1).Value) Then
        iEnd = 1
        MsgBox "No data to export.", vbInformation
        Exit Sub
    Else
        'check if column 12 has value
        If Len(ws.Cells(iRow + 1, 12).Value) = 0 Then
        ws.Cells(iRow + 1, 12).Value = ""
        Else
        ws.Cells(iRow + 1, 12).Value = ws.Cells(iRow + 1, 12).Value
        End If
        
        'check if column 13 has value
        If Len(ws.Cells(iRow + 1, 13).Value) = 0 Then
        ws.Cells(iRow + 1, 13).Value = ""
        Else
        ws.Cells(iRow + 1, 13).Value = ws.Cells(iRow + 1, 13).Value
        End If
        
        'check if column 14 has value
        If Len(ws.Cells(iRow + 1, 14).Value) = 0 Then
        ws.Cells(iRow + 1, 14).Value = ""
        Else
        ws.Cells(iRow + 1, 14).Value = ws.Cells(iRow + 1, 14).Value
        End If
        
        'check if column 15 has value
        If Len(ws.Cells(iRow + 1, 15).Value) = 0 Then
        ws.Cells(iRow + 1, 15).Value = ""
        Else
        ws.Cells(iRow + 1, 15).Value = ws.Cells(iRow + 1, 15).Value
        End If
        
        'check if column 16 has value
        If Len(ws.Cells(iRow + 1, 16).Value) = 0 Then
        ws.Cells(iRow + 1, 16).Value = ""
        Else
        ws.Cells(iRow + 1, 16).Value = ws.Cells(iRow + 1, 16).Value
        End If
        
        Print #FileNum, ws.Cells(iRow + 1, 1).Value & vbTab & ws.Cells(iRow + 1, 2).Value & vbTab & ws.Cells(iRow + 1, 3).Value & vbTab _
        & ws.Cells(iRow + 1, 4).Value & vbTab & ws.Cells(iRow + 1, 5).Value & vbTab & ws.Cells(iRow + 1, 6).Value & vbTab & ws.Cells(iRow + 1, 7).Value & vbTab _
        & ws.Cells(iRow + 1, 8).Value & vbTab & ws.Cells(iRow + 1, 9).Value & vbTab & ws.Cells(iRow + 1, 10).Value & vbTab & ws.Cells(iRow + 1, 11).Value & vbTab _
        & ws.Cells(iRow + 1, 12).Value & vbTab & ws.Cells(iRow + 1, 13).Value & vbTab & ws.Cells(iRow + 1, 14).Value & vbTab & ws.Cells(iRow + 1, 15).Value & vbTab _
        & ws.Cells(iRow + 1, 16).Value
    
    End If
    
    iRow = iRow + 1
    
    Loop
    
    Close #FileNum
    MsgBox "Please collect your file at " & DestFile, vbInformation
    
End Sub

In your if statements, use the "Exit While" part if a condition is met.

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.