Autres résultats differents entre formule et macro (excel 2007)

Boostez vos compétences Excel avec notre communauté !

Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !

gh4

XLDnaute Occasionnel
excel 2007
bonjour,
j'ai une différence de résultat quand je compare 2 cellules soit en macro soit en formule depuis la cellule et je ne sait pas pourquoi

par formule =SI(E2+F2-H2=B2;"OK";"ERREUR") tous les résultats sont bons
en macro certains résultats sont faux

Sub Macro3()
derniereligne = Cells(Rows.Count, 3).End(xlUp).Row
lign = derniereligne
colon = 3
27
'lign = 26
If lign = 1 Then GoTo 30


x = Cells(lign, colon + 2) + Cells(lign, colon + 3) - Cells(lign, colon + 5)
y = Cells(lign, colon - 1)
If x <> y Then
Cells(lign, colon + 4) = "ERREUR"
lign = lign - 1
GoTo 27
End If
If x = y Then
Cells(lign, colon + 4) = "OK"
lign = lign - 1
GoTo 27
End If
30
End Sub

si quelqu'un avait une idée.

par avance merci

Cordialement
 

Pièces jointes

Bonjour
Essayez votre code comme ceci
VB:
Sub Macro3()
Dim derniereligne As Integer, i As Integer
Dim colon As Byte
Dim x As Double, y As Double

derniereligne = Cells(Rows.Count, 3).End(xlUp).Row
colon = 3

For i = derniereligne To 2 Step -1
    x = Round(Cells(i, colon + 2) + Cells(i, colon + 3) - Cells(i, colon + 5), 2)
    y = Round(Cells(i, colon - 1), 2)
    If x <> y Then Cells(i, colon + 4) = "ERREUR" Else: Cells(i, colon + 4) = "OK"
Next i
End Sub
Cordialement
 
Pour la première différence affichant ERREUR :
'[Classeur1(39).xlsm]Feuil1'!K4, formule: =E4+F4-H4, affichée "125,21" par format "Standard",
vaut: +1,&HF4D70A3D70A3E × 2^+&H006, soit: 4405435229242655 / 2^45,
soit: 125,210000000000007958078640513122081756591796875
———————————————————————————————————
'[Classeur1(39).xlsm]Feuil1'!B4, constante affichée "125,21" par format "Standard",
vaut: +1,&HF4D70A3D70A3D × 2^+&H006, soit: 8810870458485309 / 2^46,
soit: 125,2099999999999937472239253111183643341064453125
 
Bonsoir @Dranreb, @gh4, @Dan ,Le forum

Merci pour vos réponses mais c'est du à quoi exactement cette différence les formats sont les mêmes j'ai vérifié avec nbcar alors je vois pas!

Le problème disparaît en convertissant tout en Currency :
Il fallait tous simplement typé les variables x et y en long et plus d'erreur.
comme cela :
Dim x As Long, y As Long ' x est y sont des long.
et surtout pas comme cela
Dim x, y As Long ' seulement y est long et x est un variant.
Conclusion : Pour éviter les erreurs, déclarez toujours le type de chaque variable explicitement.

VB:
Sub Macro3()
Dim x As Long, y As Long ' Pas adapté changer le Type
derniereligne = Cells(Rows.Count, 3).End(xlUp).Row
lign = derniereligne
colon = 3
27
'lign = 26
If lign = 1 Then GoTo 30


x = Cells(lign, colon + 2) + Cells(lign, colon + 3) - Cells(lign, colon + 5)
y = Cells(lign, colon - 1)
If x <> y Then
Cells(lign, colon + 4) = "ERREUR"
lign = lign - 1
GoTo 27
End If
If x = y Then
Cells(lign, colon + 4) = "OK"
lign = lign - 1
GoTo 27
End If
30
End Sub
 
Dernière édition:
Bonjour à tous,

Oui il faut arrondir pour supprimer les décimales intempestives introduites par les opérations sur les nombres.

C'est ce que fait CCur utilisé par Dranreb mais il suffit de l'appliquer uniquement au résultat de l'opération :
VB:
x = CCur(Cells(lign, colon + 2) + Cells(lign, colon + 3) - Cells(lign, colon + 5))
y = Cells(lign, colon - 1)
A+
 
Quant à la solution de laurent950 (définir x et y As Long) elle ne convient pas.

Mettez 590,6 en B26 : par macro on a "OK", par formule on a "ERREUR".

Par contre tout à fait d'accord avec :
Conclusion : Pour éviter les erreurs, déclarez toujours le type de chaque variable explicitement.
mais définir x et y As Single (c'est le mieux) ou As Currency.
 
Quant à la solution de laurent950 (définir x et y As Long) elle ne convient pas.
Pour aller plus loin :
'Nota :
' * CDec (Variant) 3,333333333333333333333333333 28 chiffres Le champion de la précision.
' * CCur 3,3333 4 Trop imprécis, Il coupe tout le reste.

VB:
Sub Macro3()
Dim x As Variant, y As Variant
derniereligne = Cells(Rows.Count, 3).End(xlUp).Row
lign = derniereligne
colon = 3
27
'lign = 26
If lign = 1 Then GoTo 30

'Nota :
'   *  CDec (Variant)  3,333333333333333333333333333   28 chiffres Le champion de la précision.
'   *  CCur    3,3333  4   Trop imprécis, Il coupe tout le reste.

'x = CDec(Cells(lign, colon + 2) + Cells(lign, colon + 3) - Cells(lign, colon + 5))
' IMPORTANT : On applique CDec sur CHAQUE cellule pour garder toute la précision
        ' durant le calcul (Col E + Col F - Col H)
        x = CDec(Cells(lign, colon + 2).Value) + _
            CDec(Cells(lign, colon + 3).Value) - _
            CDec(Cells(lign, colon + 5).Value)
'y = CDec(Cells(lign, colon - 1))
' On convertit aussi la valeur de comparaison (Col B)
        y = CDec(Cells(lign, colon - 1).Value)

If x <> y Then
Cells(lign, colon + 4) = "ERREUR"
lign = lign - 1
GoTo 27
End If
If x = y Then
Cells(lign, colon + 4) = "OK"
lign = lign - 1
GoTo 27
End If
30
End Sub
 
Dernière édition:
- Navigue sans publicité
- Accède à Cléa, notre assistante IA experte Excel... et pas que...
- Profite de fonctionnalités exclusives
Ton soutien permet à Excel Downloads de rester 100% gratuit et de continuer à rassembler les passionnés d'Excel.
Je deviens Supporter XLD

Discussions similaires

Réponses
4
Affichages
675
Réponses
8
Affichages
729
  • Question Question
XL 2021 VBA excel
Réponses
4
Affichages
424
Réponses
1
Affichages
637
Réponses
0
Affichages
632
Retour