Message macro en boucle

Bonjour

C'est encore moi.

J'ai cette macro pour vérifier les échéances à venir. Comme toujours, une erreur se glisse dans le code que je maitrise, certes, très mal :

Sub alerte()

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

Set w1 = Worksheets("Effectif") 'Feuille qui contient les alertes
D = Date
' ********************************* fin de CMU
For i = 2 To w1.Range("T" & Rows.Count).End(xlUp).Row
p = D - w1.Range("T" & i)
If p >= 0 Then MsgBox ("La CMU pour " & Cells(i, "C").Value & "  à déja expirée depuis le : " & Cells(i, "T").Value)
If p > -7 And p < 0 Then MsgBox ("La CMU pour ce jeune expirera le" & Cells(i, "T").Value)
Next i

' ********************************* fin d'ATJM
For i = 2 To w1.Range("Z" & Rows.Count).End(xlUp).Row
p = D - w1.Range("Z" & i)
If p >= 0 Then MsgBox ("L'ATJM pour " & Cells(i, "C").Value & "  à déja expirée depuis le : " & Cells(i, "Z").Value)
If p > -7 And p < 0 Then MsgBox ("Fin d'ATJM pour  " & Cells(i, "C").Value)
Next i

' ********************************* Fin d'autorisation de travail
For i = 2 To w1.Range("AM" & Rows.Count).End(xlUp).Row
p = D - w1.Range("AM" & i)
If p >= 0 Then MsgBox ("l'autorisation de travail pour :  " & Cells(i, "C").Value & "  à déja expirée depuis le : " & Cells(i, "F").Value)
Next i

End Sub

bonjour,

on doit deviner l'erreur dont il s'agit ? Ton code semble syntaxiquement correct.

j'en déduis que tu as soit une erreur à cause de tes données, soit il y a une erreur de logique. Dans les 2 cas, sans classeur et sans explications de ta part, difficile de t'aider.

Bonjour à tousse,

Si tu utilises "Option Explicit", le p n'est pas déclaré ... Dim P as .........

ric

Voici le fichier base

Bonjour,

proposition de correction

Sub alerte()

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

Set w1 = Worksheets("Effectif") 'Feuille qui contient les alertes
D = Date
' ********************************* fin de CMU
For i = 4 To w1.Range("T" & Rows.Count).End(xlUp).Row
p = D - w1.Range("T" & i)
If p >= 0 Then MsgBox ("La CMU pour " & Cells(i, "C").Value & "  a déja expiré depuis le : " & Cells(i, "T").Value)
If p > -7 And p < 0 Then MsgBox ("La CMU pour ce jeune expirera le" & Cells(i, "T").Value)
Next i

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

' ********************************* Fin d'autorisation de travail
For i = 4 To w1.Range("AM" & Rows.Count).End(xlUp).Row
p = D - w1.Range("AM" & i)
If p >= 0 Then MsgBox ("l'autorisation de travail pour :  " & Cells(i, "C").Value & "  a déja expiré depuis le : " & Cells(i, "F").Value)
Next i

End Sub

C'est déjà mieux, mais le problème se déplace, il me défile l'ensemble des situations et non celles dont la date approche

Bonjour,

de rien

en essayant de deviner ce que tu veux comme résultat

Sub alerte()

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

Set w1 = Worksheets("Effectif") 'Feuille qui contient les alertes
D = Date
' ********************************* fin de CMU
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
MsgBox ("La CMU pour " & Cells(i, "C").Value & "  a déja expiré depuis le : " & Cells(i, "T").Value)
End If
If p > -7 And p < 0 Then
MsgBox ("La CMU pour ce jeune expirera le" & Cells(i, "T").Value)
End If
End If
Next i

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

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

End Sub

Super, je te remercie du temps que tu as pris pour moi.

Rechercher des sujets similaires à "message macro boucle"