Moyenne Mobile 20
A
Salut les gars,
j'ai essayé de créer un code pour calculer une Moyenne Mobile 20. Le souci est qu'il me met qu'il y a un problème sur une ligne:
If ActiveCell.Offset(-10, 0) <> "" And ActiveCell.Offset(10, 0) <> "" Then, je ne comprends pas pourquoi...
(Je précise que ce code est en grande partie inspiré par h2so4, a qui je dis encore un grand mercii
Sub CalculeMM()
Dim i As Long, re As Range, j As Long, dl As Long
For i = 1 To 4
With Sheets(i)
.Range("H1") = "MM20" 'je renomme juste la colonne pour qu'elle est le bon nom
Set re = .Cells.Find("PX_CLOSE", lookat:=xlWhole) 'c'est la colonne à partir de laquelle je dois faire mes calculs
If re Is Nothing Then
MsgBox "colonne PX_CLOSE non trouvée dans la feuille " & .Name
Else
dl = .Cells(Rows.Count, re.Column).End(xlUp).Row
For j = re.Row + 1 To dl
If ActiveCell.Offset(-10, 0) <> "" And ActiveCell.Offset(10, 0) <> "" Then
Set selec = Range(ActiveCell.Offset(-9, 0), ActiveCell.Offset(9, 0))
somme = (.Cells(j, re.Column).Offset(-10, 0)) / 2 + (.Cells(j, re.Column).Offset(10, 0)) / 2
.Cells(j, re.Column).Offset(0, 6) = (somme + WorksheetFunction.Sum(selec)) / 20
Else: Cells(j, re.Column).Offset(0, 6) = 0
End If
Next j
End If
End With
Next i
End SubSi quelqu'un a une idée pour que ça fonctionne... ou même l'optimiser je suis preneur !!
Bonjour,
ActiveCell.Offset(-10, 0)tu as un problème quand la cellule active est sur une ligne < 10.
A
A
Bonjour,
à tester
Sub CalculeMM()
Dim i As Long, re As Range, j As Long, dl As Long, ofs As Integer
For i = 1 To 4
With Sheets(i)
.Range("H1") = "MM20" 'je renomme juste la colonne pour qu'elle est le bon nom
Set re = .Cells.Find("PX_CLOSE", lookat:=xlWhole) 'c'est la colonne à partir de laquelle je dois faire mes calculs
If re Is Nothing Then
MsgBox "colonne PX_CLOSE non trouvée dans la feuille " & .Name
Else
dl = .Cells(Rows.Count, re.Column).End(xlUp).Row
For j = re.Row + 1 To dl
If re.Row - j > -10 Then ofs = re.Row - j Else ofs = -10
If .Cells(j, re.Column).Offset(ofs, 0) <> "" And .Cells(j, re.Column).Offset(10, 0) <> "" Then
Set selec = Range(.Cells(j, re.Column).Offset(ofs + 1, 0), .Cells(j, re.Column).Offset(9, 0))
somme = (.Cells(j, re.Column).Offset(ofs, 0)) / 2 + (.Cells(j, re.Column).Offset(10, 0)) / 2
.Cells(j, re.Column).Offset(0, 6) = (somme + WorksheetFunction.Sum(selec)) / 20
Else
.Cells(j, re.Column).Offset(0, 6) = 0
End If
Next j
End If
End With
Next i
End Sub