Next sans for

Résolu
Bonjour,

Ce code ne fonctionne pas, il m'indique un next sans for en fin de code. Avez vous des idées ?

Sub alerte()

Dim w1 As Worksheet
Dim i As Long
Dim D As Date
Dim j As Integer
Dim Liste As String

Set w1 = Worksheets("Effectif") 'Feuille qui contient les alertes
D = Date

' ********************************* fin de CMU
For i = 4 To w1.Range("Y" & Rows.Count).End(xlUp).Row
ladate = w1.Range("Y" & i)
If ladate <> "" Then
p = D - ladate
If p >= 0 Then Liste = Liste & vbLf & Cells(i, "C").Value & " a déja expiré depuis le " & Cells(i, "Y")
End If
If p > -30 And p < 0 Then Liste = Liste & vbLf & "La CMU pour " & Cells(i, "C").Value & " expirera le " & Cells(i, "Y").Value
End If
End If
Next i

' ********************************* fin d'ATJM
For i = 4 To w1.Range("T" & Rows.Count).End(xlUp).Row
ladate = w1.Range("T" & i)
If ladate <> "" Then
p = D - ladate
If p >= 0 Then Liste = Liste & vbLf & "L'ATJM pour " & Cells(i, "C").Value & " a déja expiré depuis le " & Cells(i, "T").Value
End If
If p > -30 And p < 0 Then Liste = Liste & vbLf & "L'ATJM pour " & Cells(i, "C").Value & " expirera le " & Cells(i, "T").Value
End If
End If
Next i

' ********************************* Fin d'autorisation de travail
For i = 4 To w1.Range("AU" & Rows.Count).End(xlUp).Row
ladate = w1.Range("AU" & i)
If ladate <> "" Then
p = D - ladate
If p >= 0 Then Liste = Liste & vbLf & "L'autorisation de travail pour " & Cells(i, "C").Value & " a déja expiré depuis le " & Cells(i, "AU").Value
End If
If p > -30 And p < 0 Then Liste = Liste & vbLf & "L'autorisation de travail pour " & Cells(i, "C").Value & " expirera le " & Cells(i, "AU").Value
End If
End If
Next i

Next

MsgBox Liste, vbOKOnly + vbInformation, "Alertes"

End Sub



Configuration: Windows / Chrome 81.0.4044.113

2 réponses

  1. Bonjour
    Teste plutôt comme ceci
    Tu avais des End If ainsi que le Next à la fin inutiles

    Sub alerte()
    
    Dim w1 As Worksheet
    Dim i As Long
    Dim D As Date
    Dim j As Integer
    Dim Liste As String
    
    Set w1 = Worksheets("Effectif") 'Feuille qui contient les alertes
    D = Date
    
    
    ' ********************************* fin de CMU
      For i = 4 To w1.Range("Y" & Rows.Count).End(xlUp).Row
      ladate = w1.Range("Y" & i)
        If ladate <> "" Then
          p = D - ladate
          If p >= 0 Then Liste = Liste & vbLf & Cells(i, "C").Value & " a déja expiré depuis le " & Cells(i, "Y")
          If p > -30 And p < 0 Then Liste = Liste & vbLf & "La CMU pour " & Cells(i, "C").Value & " expirera le " & Cells(i, "Y").Value
        End If
      Next i
    
    ' ********************************* fin d'ATJM
      For i = 4 To w1.Range("T" & Rows.Count).End(xlUp).Row
        ladate = w1.Range("T" & i)
        If ladate <> "" Then
          p = D - ladate
          If p >= 0 Then Liste = Liste & vbLf & "L'ATJM pour " & Cells(i, "C").Value & " a déja expiré depuis le " & Cells(i, "T").Value
          If p > -30 And p < 0 Then Liste = Liste & vbLf & "L'ATJM pour " & Cells(i, "C").Value & " expirera le " & Cells(i, "T").Value
        End If
      Next i
    
    ' ********************************* Fin d'autorisation de travail
      For i = 4 To w1.Range("AU" & Rows.Count).End(xlUp).Row
      ladate = w1.Range("AU" & i)
        If ladate <> "" Then
          p = D - ladate
          If p >= 0 Then Liste = Liste & vbLf & "L'autorisation de travail pour " & Cells(i, "C").Value & " a déja expiré depuis le " & Cells(i, "AU").Value
          If p > -30 And p < 0 Then Liste = Liste & vbLf & "L'autorisation de travail pour " & Cells(i, "C").Value & " expirera le " & Cells(i, "AU").Value
        End If
      Next i
    
    MsgBox Liste, vbOKOnly + vbInformation, "Alertes"
    
    End Sub
    
    1. Merci de ton aide. Mais du coup, un autre problème de type incompatibilité de type 13 pour le dernier p = D - ladate

      La colonne "AU" est bien au format date. Il n'y a pas de différences notables avec la première partie du programme
    2. @M-12Comment insérer un fichier dans ce forum ?