Bonjour,

j'utilise une fonction qui me permet de calculer un prix moyen pondéré à partir d'un historique de prix.

Tout fonctionne très bien mais je voudrais améliorer juste un point : la couleur des cellules utilisées pour calculer ce prix moyen pondéré.

Je me demande si il n'y a pas une quelconque limitation.

voici le code :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
Public x

Function GetPrice(isin, horodatage, quantity)

Dim colQuant, colPrix, quantTemp, prixTemp, colFutures

colPrix = 2
colQuant = 3

'On va déterminer où se trouve l'histo

'Colonne
Set FEUIL = ThisWorkbook.Sheets("HISTO")
Set x = FEUIL.range("1:1").Find(isin)
colFutures = x.Column

'Ligne
horodatageTemp = Format(horodatage, "dd/mm/yyyy hh:mm")
Set y = range(FEUIL.Cells(2, colFutures), FEUIL.Cells(2, colFutures).End(xlDown))

Call getLigne(horodatageTemp, y)

rowTemp = x.Row
Do While FEUIL.Cells(rowTemp, colFutures) < horodatage
 rowTemp = rowTemp + 1
Loop
FirstRow = rowTemp
  Do While quantTemp + FEUIL.Cells(rowTemp, colFutures + colQuant) <= quantity
  
  Call formatRange(FEUIL.Cells(rowTemp, colFutures))
    quantTemp = quantTemp + FEUIL.Cells(rowTemp, colFutures + colQuant)
    prixTemp = prixTemp + FEUIL.Cells(rowTemp, colFutures + colPrix) * FEUIL.Cells(rowTemp, colFutures + colQuant)
    rowTemp = rowTemp + 1
  Loop
  If quantTemp <> quantity Then
  
  residu = quantity - quantTemp
  prixTemp = prixTemp + FEUIL.Cells(rowTemp, colFutures + colPrix) * residu
  End If


GetPrice = Round(prixTemp / quantity, 4)

End Function


Sub getLigne(data, r)
Set x = r.Find(what:=data, LookIn:=xlValues)
Do While x Is Nothing
  data = Format(data, "dd/mm/yyyy hh")
      Set x = r.Find(what:=data, LookIn:=xlValues)
Loop
End Sub


Sub formatRange(r)
r.Color = 3
End Sub
J'ai un probleme sur la ligne rouge quand on fait appel à la sub formatRange
Ce qui m'étonne c'est que cela fonctionne lorsque je fais appel à cette sub à partir d'une autre sub.

Merci d'avance