Aide pour synthetiser
Bonjour,
Je vous sollicites, car j'écris du code et du code mais je me doutes qu'il y a moyen de synthétiser, grandement ce que j'écris, pourriez vous me dire si le code suivant peut se synthétiser, et auquel cas comment vous vous y prendriez?
Il s'agit de trois boucles agissant de la même manière, je me doutes qu'il y a quelque chose à faire mais quoi?
Merci par avance.
'CALCUL DES STATS EN NOMBRE
For j = 3 To 25
c = 0
g = 0
o = 0
For i = 2 To Sheets("CA").Range("A" & Rows.Count).End(xlUp).Row
sem = Sheets("CA").Cells(i, 1).Value
sema = DatePart("yyyy", sem) & DatePart(Periode, sem, vbMonday, vbFirstFourDays)
If sema = q Then
c = Sheets("CA").Cells(i, j).Value + c
End If
If sema = z Then
g = Sheets("CA").Cells(i, j).Value + g
End If
If sema = t Then
o = Sheets("CA").Cells(i, j).Value + o
End If
If Periode = "y" Then
Sheets("STATS").Cells(j - 1, 2) = c
Sheets("STATS").Cells(j - 1, 3) = g
Sheets("STATS").Cells(j - 1, 4) = o
End If
If Periode = "ww" Then
Sheets("STATS").Cells(j - 1, 5) = c
Sheets("STATS").Cells(j - 1, 6) = g
Sheets("STATS").Cells(j - 1, 7) = o
End If
If Periode = "m" Then
Sheets("STATS").Cells(j - 1, 8) = c
Sheets("STATS").Cells(j - 1, 9) = g
Sheets("STATS").Cells(j - 1, 10) = o
End If
If Periode = "q" Then
Sheets("STATS").Cells(j - 1, 11) = c
Sheets("STATS").Cells(j - 1, 12) = g
Sheets("STATS").Cells(j - 1, 13) = o
End If
If Periode = "yyyy" Then
Sheets("STATS").Cells(j - 1, 14) = c
Sheets("STATS").Cells(j - 1, 15) = g
End If
Next i
Next j
'CALCUL DES STATS EN CA
For j = 30 To 52
c = 0
g = 0
o = 0
For i = 2 To Sheets("CA").Range("A" & Rows.Count).End(xlUp).Row
sem = Sheets("CA").Cells(i, 1).Value
sema = DatePart("yyyy", sem) & DatePart(Periode, sem, vbMonday, vbFirstFourDays)
If sema = q Then
c = Sheets("CA").Cells(i, j).Value + c
End If
If sema = z Then
g = Sheets("CA").Cells(i, j).Value + g
End If
If sema = t Then
o = Sheets("CA").Cells(i, j).Value + o
End If
l = Sheets("CA").Cells(1, j).Value
If Periode = "y" Then
Sheets("STATS").Cells(j - 28, 16) = c
Sheets("STATS").Cells(j - 28, 17) = g
Sheets("STATS").Cells(j - 28, 18) = o
End If
If Periode = "ww" Then
Sheets("STATS").Cells(j - 28, 19) = c
Sheets("STATS").Cells(j - 28, 20) = g
Sheets("STATS").Cells(j - 28, 21) = o
End If
If Periode = "m" Then
Sheets("STATS").Cells(j - 28, 22) = c
Sheets("STATS").Cells(j - 28, 23) = g
Sheets("STATS").Cells(j - 28, 24) = o
End If
If Periode = "q" Then
Sheets("STATS").Cells(j - 28, 25) = c
Sheets("STATS").Cells(j - 28, 26) = g
Sheets("STATS").Cells(j - 28, 27) = o
End If
If Periode = "yyyy" Then
Sheets("STATS").Cells(j - 28, 28) = c
Sheets("STATS").Cells(j - 28, 29) = g
End If
Next i
Next j
'CALCUL DES STATS EN FRAIS
For j = 54 To 75
c = 0
g = 0
o = 0
For i = 2 To Sheets("CA").Range("A" & Rows.Count).End(xlUp).Row
sem = Sheets("CA").Cells(i, 1).Value
sema = DatePart("yyyy", sem) & DatePart(Periode, sem, vbMonday, vbFirstFourDays)
If sema = q Then
c = Sheets("CA").Cells(i, j).Value + c
End If
If sema = z Then
g = Sheets("CA").Cells(i, j).Value + g
End If
If sema = t Then
o = Sheets("CA").Cells(i, j).Value + o
End If
l = Sheets("CA").Cells(1, j).Value
If Periode = "y" Then
Sheets("STATS").Cells(j - 52, 30) = c
Sheets("STATS").Cells(j - 52, 31) = g
Sheets("STATS").Cells(j - 52, 32) = o
End If
If Periode = "ww" Then
Sheets("STATS").Cells(j - 52, 33) = c
Sheets("STATS").Cells(j - 52, 34) = g
Sheets("STATS").Cells(j - 52, 35) = o
End If
If Periode = "m" Then
Sheets("STATS").Cells(j - 52, 36) = c
Sheets("STATS").Cells(j - 52, 37) = g
Sheets("STATS").Cells(j - 52, 38) = o
End If
If Periode = "q" Then
Sheets("STATS").Cells(j - 52, 39) = c
Sheets("STATS").Cells(j - 52, 40) = g
Sheets("STATS").Cells(j - 52, 41) = o
End If
If Periode = "yyyy" Then
Sheets("STATS").Cells(j - 52, 42) = c
Sheets("STATS").Cells(j - 52, 43) = g
End If
Next i
Next jBonsoir,
À première vue, tu pourrais utiliser un Select pour vérifier les valeurs, plutôt que plein de If, mais j'ai juste jeté un oeil à ton code.
Un exemple dans le cours du site :
Sub commentaires_notes()
'Variables
Dim note As Integer, commentaire As String
note = Range("A1")
'Commentaire en fonction de la note
Select Case note ' <= la valeur à tester (ici, la note)
Case Is = 6 ' <= si la valeur = 6
commentaire = "Excellent résultat !"
Case Is = 5 ' <= si la valeur = 5
commentaire = "Bon résultat"
Case Is = 4 ' <= si la valeur = 4
commentaire = "Résultat satisfaisant"
Case Is = 3 ' <= si la valeur = 3
commentaire = "Résultat insatisfaisant"
Case Is = 2 ' <= si la valeur = 2
commentaire = "Mauvais résultat"
Case Is = 1 ' <= si la valeur = 1
commentaire = "Résultat exécrable"
Case Else ' <= si la valeur n'est égale à aucune des valeurs ci-dessus
commentaire = "Aucun résultat"
End Select
'Commentaire en B1
Range("B1") = commentaire
End Subsur la page https://www.excel-pratique.com/fr/vba/conditions.php
Bonsoir,
Ce n'est pas beau mais ça marche non ?
'CALCUL DES STATS EN NOMBRE
For j = 3 To 75
If j = 26 Then
j = 30 'CALCUL DES STATS EN CA
ElseIf j = 53 Then
j = 54 'CALCUL DES STATS EN FRAIS
End If
c = 0
g = 0
o = 0
For i = 2 To Sheets("CA").Range("A" & Rows.Count).End(xlUp).Row
sem = Sheets("CA").Cells(i, 1).Value
sema = DatePart("yyyy", sem) & DatePart(Periode, sem, vbMonday, vbFirstFourDays)
If sema = q Then
c = Sheets("CA").Cells(i, j).Value + c
End If
If sema = Z Then
g = Sheets("CA").Cells(i, j).Value + g
End If
If sema = t Then
o = Sheets("CA").Cells(i, j).Value + o
End If
If Periode = "y" Then
Sheets("STATS").Cells(j - 1, 2) = c
Sheets("STATS").Cells(j - 1, 3) = g
Sheets("STATS").Cells(j - 1, 4) = o
End If
If Periode = "ww" Then
Sheets("STATS").Cells(j - 1, 5) = c
Sheets("STATS").Cells(j - 1, 6) = g
Sheets("STATS").Cells(j - 1, 7) = o
End If
If Periode = "m" Then
Sheets("STATS").Cells(j - 1, 8) = c
Sheets("STATS").Cells(j - 1, 9) = g
Sheets("STATS").Cells(j - 1, 10) = o
End If
If Periode = "q" Then
Sheets("STATS").Cells(j - 1, 11) = c
Sheets("STATS").Cells(j - 1, 12) = g
Sheets("STATS").Cells(j - 1, 13) = o
End If
If Periode = "yyyy" Then
Sheets("STATS").Cells(j - 1, 14) = c
Sheets("STATS").Cells(j - 1, 15) = g
End If
Next i
Next jOn teste la valeur de j, on décale cette valeur afin de reprendre la boucle à une donnée plus loin...
@ bientôt
LouReeD
Ok alors d'abord merci pour vos deux réponses, pour select, c'est ok j'ai compris c'est quelque chose que j'utiliserai dorénavant.
Après voilà mes pistes pour synthétiser, la première, c'est que c,g,et o, c'est pareil, il me semble donc que je peux n'utiliser qu'une variable, et ensuite je pense a un tableau (array), dans lequel il y aurait, finalement les conditions...
Mais j'ai essayé plein de choses, et je n'y arrive pas.