If dans boucle For et recours à Next dans le If

Résolu/Fermé
Barthe - 24 juil. 2014 à 18:52
 Barthe - 25 juil. 2014 à 19:21
Bonjour tout le monde,

Est-il possible de mettre le next i d'un For dans un If se situant dans le For. Par exemple :


For i = 0 to 55
If (i Mod 2 = 0) then
'procedure quelconque
Next i
Else
'on ne fait rien
End If
Next i

Je vous pose cette question car je viens de coder une procédure avec plein de For et de If et je crains que le code ne bloque à cause d'un If imbriqué dans un For et faisant appel (si condition vérifié) au Next du For.

Voila le code en question, il est vraiment lourd mais je l'ai épuré et commenté pour vous faciliter la tâche. Le compilateur me renvoit comme erreur Next sans For (car le Next est dans un If je pense).

J'espère que vous pourrez m'aider :)



'procedure that update the database by treating all the new datas
Sub updateall()

Dim i As Long

For i = 3 To number_of_line 'variable déclaré en début de module, valant 18

'function that decides if the patient whose number of line is "i" is new or not and return a string "newpatient" or "existingpatient" (with his position positionlinepatientexisting in the database)
Dim typeofpatient As String
Dim positionlinepatientexisting As Integer
Dim j As Long
Dim security As Integer
Dim piniciales As Integer
Dim afiliation As Integer
Dim institution As Integer

If table(i - 2, positioncolumn - 1) = "" Then
'table est un table crée en début de module et de taille number_of_line (18), number_of_column (56)
'positioncolumn est déclaré en début de module et vaut 7

security = security + 1
Else
End If

If table(i - 2, positionafiliation - 1) = "" Then
'Idem, positionafiliation déclaré en début de module et vaut 8
security = security + 1
Else
End If

If table(i - 2, positioninstitution - 1) = "" Then
'Idem, positioninstitution déclaré en début de module et vaut 2
security = security + 1
Else
End If

If security = 3 Then
errorupdate (i) 'autre procédure qui copie colle la ligne i dans une autre feuille (feuille Error)
MsgBox "We don't know the P. INICIALES, the AFILIACION and the INSTITUTION of the patient number : " & i & "." & Chr(10) & "Thus, we can't add him to the database." & Chr(10) & Chr(10) & "The patient has been copied in the sheet Error." & Chr(10) & "Please, deal with it manually at the end of the updating."
Next i

Else

'We identify the patient in the database and determine if he is new or not
If table(i - 2, positioncolumn - 1) <> "" Then
For j = 1 To number_of_line_in_the_database - 2
'De même que pour table, base est un "table" déclaré en début de module de taille 'number_of_line_in_the_database (656), number_of_column_in_the_database (56) ou 'number_of_line_in_the_database et number_of_column_in_the_database sont deux variables 'déclarés en début de module
If base(j, positioncolumndatabase - 1) = table(i - 2, positioncolumn - 1) Then
piniciales = j
Exit For
ElseIf j = number_of_line_in_the_database - 2 Then
typeofpatient = "newpatient"
Else
End If
Next j
Else
End If

'Second security by identifying the afiliation of the patient
If table(i - 2, positionafiliation - 1) <> "" Then
For j = 1 To number_of_line_in_the_database - 2
If base(j, positionafiliationdatabase - 1) = table(i - 2, positionafiliation - 1) Then
afiliation = j
Exit For
ElseIf j = number_of_line_in_the_database - 2 Then
typeofpatient = "newpatient"
Else
End If
Next j
Else
End If

'Third and last security by identifying the institution of the patient
If table(i - 2, positioninstitution - 1) <> "" Then
For j = 1 To number_of_line_in_the_database - 2
If base(j, positioninstitutiondatabase - 1) = table(i - 2, positioninstitution - 1) Then
institution = j
Exit For
Else
End If
Next j
Else
End If

If piniciales <> afiliation Then
errorupdate (i)
MsgBox "Error with the patient number : " & i & "." & Chr(10) & "We can't add him to the database." & Chr(10) & Chr(10) & "The patient has been copied in the sheet Error." & Chr(10) & "Please, deal with it manually at the end of the updating."
Next i
ElseIf piniciales <> institution Then
errorupdate (i)
MsgBox "Error with the patient number : " & i & "." & Chr(10) & "We can't add him to the database." & Chr(10) & Chr(10) & "The patient has been copied in the sheet Error." & Chr(10) & "Please, deal with it manually at the end of the updating."
Next i
ElseIf afiliation <> institution Then
errorupdate (i)
MsgBox "Error with the patient number : " & i & "." & Chr(10) & "We can't add him to the database." & Chr(10) & Chr(10) & "The patient has been copied in the sheet Error." & Chr(10) & "Please, deal with it manually at the end of the updating."
Next i
Else
If piniciales <> 0 Then
positionlinepatientexisting = piniciales + 2
typeofpatient = "existingpatient"
ElseIf afiliation <> 0 Then
positionlinepatientexisting = afiliation + 2
typeofpatient = "existingpatient"
ElseIf institution <> 0 Then
positionlinepatientexisting = institution + 2
typeofpatient = "existingpatient"
Else
End If

If security = 2 Then
If MsgBox("Attention : we could only use one parameter among P.INICIALES, AFILIACION and INSTITUTION in order to identify the patient number " & i & " !" & Chr(10) & Chr(10) & Chr(10) & "Do you prefer to deal with this patient manually ?", vbYesNo, "Request for confirmation") = vbYes Then
errorupdate (i)
MsgBox "You will be able to deal with it manually just after that the system updates the other patients, as the information have been copied in the sheet Error."
Next i
Else
End If
Else
End If
End If

'launch the procedure patientnew
If typeofpatient = "newpatient" Then
'procedure patientnew
Call patientnew(i)
Next i

'launch the procedure patientexisting
ElseIf typeofpatient = "existingpatient" Then
'procedure patientexisting
Call patientexisting(i, positionlinepatientexisting)
Next i

'case of error
Else
'procedure à checker !
errorupdate (i)
MsgBox "Error with the patient number " & i & "." & Chr(10) & "The information have been copied in the sheet Error." & Chr(10) & "Please, deal with it manually."
Next i
End If

End If


A voir également:

2 réponses

Iama Messages postés 319 Date d'inscription mercredi 13 janvier 2010 Statut Membre Dernière intervention 27 mars 2020 14
Modifié par Iama le 25/07/2014 à 11:26
Bonjour

Bien que le goto est déconseillé par beaucoup, associé à une étiquette je ne vois pas de raison à ne pas l'utiliser.

Je te propose :

For i = 0 To 55
If (i = mods) Then
'procedure quelconque
GoTo sauT
Else
'on ne fait rien
End If

sauT:
Next i

En espérant que cela convienne
PS: je n'ai pas tout ton code
0
Merci pour la réponse !

Ce n'était pas vraiment ce que je cherchais mais ta proposition m'a permis de trouver une solution. J'ai tout simplement sorti la boucle for de ma procédure (au lieu de faire une procédure avec un for dedans, je fais deux procédure : la première procédure comprenant le for et le for qui appelle la seconde procédure à chaque itération).

Merci :)
0