Non prise en compte de l'incrément d'une boucle "for"
Bonjour,
Je viens faire appel à votre savoir car je n'ai pas trouvé de solution à mon problème sur le toile...
Je souhaite effectuer le calcul d'un score en fonction de critère et du niveau de maitrise de ces critères. pour ce faire j'utilise la fonction suivante :
Option Explicit
Sub calcul_score()
Application.ScreenUpdating = False
Dim ref_fin As Integer
Dim current_project As Integer
Dim current_critere As Integer
Dim current_aspect As Integer
Dim coeff_expert As Single
Dim coef_inter As Single
Dim coef_novice As Single
Dim score As Integer
Dim next_cal As Integer
ref_fin = Sheets("Référencement").Range("B" & Rows.Count).End(xlUp).Row
coeff_expert = Sheets("Recherche").Cells(1, 11)
coef_inter = Sheets("Recherche").Cells(2, 11)
coef_novice = Sheets("Recherche").Cells(3, 11)
next_cal = 3
For current_project = 7 To ref_fin
score = 0
For current_critere = 2 To 16
If IsEmpty(Sheets("Recherche").Cells(6, current_critere)) Then
Exit For
Else
For current_aspect = 4 To 101 Step next_cal
If Sheets("Recherche").Cells(6, current_critere) = _
Sheets("Référencement").Cells(current_project, current_aspect) Then
If Sheets("Référencement").Cells(current_project, current_aspect + 1) = "Expert" Then
score = score + (coeff_expert * Sheets("Recherche").Cells(7, current_critere))
Else
If Sheets("Référencement").Cells(current_project, current_aspect + 1) = "Intermédiaire" Then
score = score + (coef_inter * Sheets("Recherche").Cells(7, current_critere))
Else
If Sheets("Référencement").Cells(current_project, current_aspect + 1) = "Novice" Then
score = score + (coef_novice * Sheets("Recherche").Cells(7, current_critere))
End If
End If
End If
End If
If current_aspect = 16 Then
next_cal = 2
Else: next_cal = 3
End If
Next current_aspect
End If
Next current_critere
Sheets("Recherche").Cells(current_project + 4, 5) = score
Next current_project
Application.ScreenUpdating = True
End SubMon soucis c'est qu'une fois que l'on atteint la colonne 16 ("P"), mon incrément reste sur 3 et donc je tombe sur la colonne 19 ("S") au lieu de la 18 ("R"), la suite de mon calcul tombe à l'eau.
Est-ce que l'on d'entre vous saurais pourquoi ?
J'ai bien essayé de supprimer la première définition de "next_cal" mais le pas devient alors égale à 0 et je reste bloqué sur ma première colonne.
J'ai aussi tenté de bouger mon test sur le numéros de colonne, juste avant puis après l'instruction "for", rien à faire...
J'ai pensé que cela pouvais venir de mon test mais avec une autre forme rien à faire c'est pareil.
If current_aspect <> 16 Then
next_cal = 3
Else: next_cal = 2
End IfAvez-vous déjà rencontré ce problème ou percevez-vous une solution ?
Merci pour l'aide !
Bonjour,
Tu ne peux pas procéder de la sorte car la variable Next_Cal est définie au début du code et une fois la boucle FOR exécutée cette variable ne sera pas modifiée
Essaie plutot comme ceci :
Option Explicit
Sub calcul_score()
Application.ScreenUpdating = False
Dim ref_fin As Integer
Dim current_project As Integer
Dim current_critere As Integer
Dim current_aspect As Integer
Dim coeff_expert As Single
Dim coef_inter As Single
Dim coef_novice As Single
Dim score As Integer
Dim next_cal As Byte
ref_fin = Sheets("Référencement").Range("B" & Sheets("Référencement").Rows.Count).End(xlUp).Row
coeff_expert = Sheets("Recherche").Cells(1, 11)
coef_inter = Sheets("Recherche").Cells(2, 11)
coef_novice = Sheets("Recherche").Cells(3, 11)
next_cal = 3
current_aspect = 4
For current_project = 7 To ref_fin
score = 0
For current_critere = 2 To 16
If IsEmpty(Sheets("Recherche").Cells(6, current_critere)) Then
Exit For
Else
Do While current_aspect <= 101
If Sheets("Recherche").Cells(6, current_critere) = _
Sheets("Référencement").Cells(current_project, current_aspect) Then
If Sheets("Référencement").Cells(current_project, current_aspect + 1) = "Expert" Then
score = score + (coeff_expert * Sheets("Recherche").Cells(7, current_critere))
Else
If Sheets("Référencement").Cells(current_project, current_aspect + 1) = "Intermédiaire" Then
score = score + (coef_inter * Sheets("Recherche").Cells(7, current_critere))
Else
If Sheets("Référencement").Cells(current_project, current_aspect + 1) = "Novice" Then
score = score + (coef_novice * Sheets("Recherche").Cells(7, current_critere))
End If
End If
End If
End If
If current_aspect = 16 Then next_cal = 2
current_aspect = current_aspect + next_cal
Loop
End If
Next current_critere
Sheets("Recherche").Cells(current_project + 4, 5) = score
Next current_project
Application.ScreenUpdating = True
End SubSi ok, clique sur le v vert à coté du bouton EDITER lors de ta réponse afin de cloturer le fil
Cordialement
Bonjour Dan et merci pour ta réponse.
J'ai modifié mon code hier soir pour enfin obtenir ce que je souhaite.
Je me base encore sur une coucle "for" sauf que je m'appuie maintenant sur ma variable d'incrémentation comme ceci :
If current_aspect = 16 Then
current_aspect = 15
Else: next_cal = 3
End IfDu coup je n'ai plus de soucis mais certains trouverons peut être que ce n'est pas très "propre" comme code.
Encore merci pour ta réponse.
Re,
Ok. C'est ausi une solution mais le ELSE ne sert à rien
Mets comme ceci -->
If current_aspect = 16 Then current_aspect = 15Plutot que :
If current_aspect = 16 Then
current_aspect = 15
Else: next_cal = 3
End IfAmicalement
Ok
Ca marche toujours, c'est nickel et encore merci !
Cdlt, Cesz