Elielton Limeira
Elielton Limeira

Reputation: 25

For inside another For, Next without For Error

For i = 0 To lQtd
    ...

    confemails = Split(EnviarPara, ";")

    For j = 0 To UBound(confemails)
        If EmailValido(confemails(j)) Then

             Next j
        
        Else
            MsgBox "O e-mail" & confemails(j) & "Na linha" & i & "da tabela, foi digitado incorretamente, favor corrigir e enviar novamente."

            Next i

        EndIf

        SendEmail

Next i

My problem is in the second For.

The idea is to go through the list of email addresses (confemails) to make sure all email addresses are in the correct format.

If so: send email
If not: send a message and go to next list of email addresses

compilation Error: Next without For

Compilation Error: Next without For

Upvotes: 0

Views: 83

Answers (2)

CDP1802
CDP1802

Reputation: 16392

For i = 0 To lQtd
    ...

    confemails = Split(EnviarPara, ";")
    msg = ""
    For j = 0 To UBound(confemails)
        If Not EmailValido(confemails(j)) Then
             msg = msg & " " & confemails(j)
        End If
    Next

    If msg = "" Then
        Sendmail
    Else    
        MsgBox "O e-mail Na linha " & i & " da tabela, foi digitado incorretamente, favor corrigir e enviar novamente." & msg
    End If
Next

Upvotes: 2

Scott Craner
Scott Craner

Reputation: 152660

You cannot have the Next inside the If block. Instead just do the Else:

For i = 0 To lQtd
    ...

    confemails = Split(EnviarPara, ";")

    For j = 0 To UBound(confemails)
        If Not EmailValido(confemails(j)) Then
            MsgBox "O e-mail" & confemails(j) & "Na linha" & i & "da tabela, foi digitado incorretamente, favor corrigir e enviar novamente."
            Exit For
        End If        
        SendEmail
    Next j
Next i

Upvotes: 3

Related Questions