Double clic sur Calculer en VBA

bonsoir

j'ai un problème avec ma macro, pour obtenir les résultats justes je dois cliquer deux fois sur mon bouton calculer pour obtenir les bons résultats sur la colonne D.

voila mon code

Code :
Sub calCoeff()

    Dim i As Integer
    Dim DerLig As Long

    With Worksheets("Moyenne_Mobile")
        DerLig = .Range("A" & .Rows.Count).End(xlUp).Row

        For i = 2 To DerLig - 2
            .Cells(i + 1, 3).Value = Application.WorksheetFunction.Average(.Range(.Cells(i, 2), .Cells(i + 3, 2)))
            .Cells(i + 2, 4).Value = Application.WorksheetFunction.Average(.Range(.Cells(i + 1, 3), .Cells(i + 2, 3)))
        Next i
         .Range(.Cells(DerLig - 1, 4), .Cells(DerLig, 4)).ClearContents
    End With

End Sub

merci pour votre aide

cordialement

xi yi Moyenne mobile Moyenne mobile centrée

1 6000

2 4500 4250,00

3 1500 4625,00 4437,50

4 5000 4700,00 4662,50

5 7500 4825,00 4762,50

6 4800 4950,00 4887,50

7 2000 5325,00 5137,50

8 5500 5675,00 5500,00

9 9000 5650,00 5662,50

10 6200 5475,00 5562,50

11 1900 5425,00 5450,00

12 4800 5275,00 5350,00

13 8800 5225,00 5250,00

14 5600 5100,00 5162,50

15 1700 3866,67

16 4300

15moyenn.zip (8.49 Ko)

Salut le forum

Utilise deux boucles

Sub calCoeff()

    Dim i As Integer
    Dim DerLig As Long

    With Worksheets("Moyenne_Mobile")
        DerLig = .Range("A" & .Rows.Count).End(xlUp).Row

        For i = 2 To DerLig - 2
            .Cells(i + 1, 3).Value = Application.WorksheetFunction.Average(.Range(.Cells(i, 2), .Cells(i + 3, 2)))
        Next i
        For i = 2 To DerLig - 2
            .Cells(i + 2, 4).Value = Application.WorksheetFunction.Average(.Range(.Cells(i + 1, 3), .Cells(i + 2, 3)))
        Next i
         .Range(.Cells(DerLig - 1, 4), .Cells(DerLig, 4)).ClearContents
    End With

End Sub

Mytå

Bonsoir

Bonsoir Mytå

A première vue tu te sers d'informations non calculées lors de du calcul de ta "Moyenne mobile centrée "

Une solution faire 2 boucles

Sub calCoeff()
    Dim i As Integer
    Dim DerLig As Long

    With Worksheets("Moyenne_Mobile")
        DerLig = .Range("A" & .Rows.Count).End(xlUp).Row
        For i = 2 To DerLig - 2
            .Cells(i + 1, 3).Value = Application.WorksheetFunction.Average(.Range(.Cells(i, 2), .Cells(i + 3, 2)))
        Next i
        For i = 2 To DerLig - 2
            .Cells(i + 2, 4).Value = Application.WorksheetFunction.Average(.Range(.Cells(i + 1, 3), .Cells(i + 2, 3)))
        Next i
         .Range(.Cells(DerLig - 1, 4), .Cells(DerLig, 4)).ClearContents
    End With
End Sub
Rechercher des sujets similaires à "double clic calculer vba"