Résultat incohérent avec excel en VBA
polohpi
-
polohpi -
polohpi -
Bonjour,
voici mon problème : J'ai une macro qui fait des soustractions avec par exemple
68,58 - 71,46
Il me sort -2,879997. Sérieux comment excel peut trouver ca. Ca me rend fou. Que faire ?
Merci
ps : voila le code complet :
voici mon problème : J'ai une macro qui fait des soustractions avec par exemple
68,58 - 71,46
Il me sort -2,879997. Sérieux comment excel peut trouver ca. Ca me rend fou. Que faire ?
Merci
ps : voila le code complet :
Sub etude()
Dim moy_ibis_orleans As Single, moy_marg As Single, moy_st_aignan As Single, moy_ibis_bud As Single, moy_ball As Single, moy_apart As Single, moy_adagio As Single, moy_s_et_a As Single
Dim moy_ibis_orleans2 As Single, moy_marg2 As Single, moy_st_aignan2 As Single, moy_ibis_bud2 As Single, moy_ball2 As Single, moy_apart2 As Single, moy_adagio2 As Single, moy_s_et_a2 As Single
Dim diff_moy_ibis_orleans As Single, diff_moy_marg As Single, diff_moy_st_aignan As Single, diff_moy_ibis_bud As Single, diff_moy_ball As Single, diff_moy_apart As Single, diff_moy_adagio As Single, diff_moy_s_et_a As Single
Worksheets("02.02.2014").Activate
moy_ibis_orleans = Cells(10, 2)
moy_marg = Cells(10, 3)
moy_st_aignan = Cells(10, 4)
moy_ibis_bud = Cells(10, 5)
moy_ball = Cells(10, 6)
moy_apart = Cells(10, 7)
moy_adagio = Cells(10, 8)
moy_s_et_a = Cells(10, 9)
Worksheets("26.01.2014").Activate
moy_ibis_orleans2 = Cells(9, 2)
moy_marg2 = Cells(9, 3)
moy_st_aignan2 = Cells(9, 4)
moy_ibis_bud2 = Cells(9, 5)
moy_ball2 = Cells(9, 6)
moy_apart2 = Cells(9, 7)
moy_adagio2 = Cells(9, 8)
moy_s_et_a2 = Cells(9, 9)
Worksheets("02.02.2014").Activate
diff_moy_ibis_orleans = moy_ibis_orleans - moy_ibis_orleans2
diff_moy_marg = moy_marg - moy_marg2
diff_moy_st_aignan = moy_st_aignan - moy_st_aignan2
diff_moy_ibis_bud = moy_ibis_bud - moy_ibis_bud2
diff_moy_ball = moy_ball - moy_ball2
diff_moy_apart = moy_apart - moy_apart2
diff_moy_adagio = moy_adagio - moy_adagio2
diff_moy_s_et_a = moy_s_et_a - moy_s_et_a2
If moy_ibis_orleans > moy_ibis_orleans2 Then 'ibis orleans
Cells(11, 2) = "+ " & Abs(diff_moy_ibis_orleans) & " €"
Cells(11, 2).Font.ColorIndex = 10
ElseIf moy_ibis_orleans < moy_ibis_orleans2 Then
Cells(11, 2) = "- " & Abs(diff_moy_ibis_orleans) & " €"
Cells(11, 2).Font.ColorIndex = 3
ElseIf moy_ibis_orleans = moy_ibis_orleans2 Then
Cells(11, 2) = "/"
End If
If moy_marg > moy_marg2 Then 'hotel margeurite
Cells(11, 3) = "+ " & Abs(diff_moy_marg) & " €"
Cells(11, 3).Font.ColorIndex = 10
ElseIf moy_marg < moy_marg2 Then
Cells(11, 3) = "- " & Abs(diff_moy_marg) & " €"
Cells(11, 3).Font.ColorIndex = 3
ElseIf moy_marg = moy_marg2 Then
Cells(11, 3) = "/"
End If
If moy_st_aignan > moy_st_aignan2 Then 'st aignan
Cells(11, 4) = "+ " & Abs(diff_moy_st_aignan) & " €"
Cells(11, 4).Font.ColorIndex = 10
ElseIf moy_st_aignan < moy_st_aignan2 Then
Cells(11, 4) = "- " & Abs(diff_moy_st_aignan) & " €"
Cells(11, 4).Font.ColorIndex = 3
ElseIf moy_st_aignan = moy_st_aignan2 Then
Cells(11, 4) = "/"
End If
If moy_ibis_bud > moy_ibis_bud2 Then 'ibis budget
Cells(11, 5) = "+ " & Abs(diff_moy_ibis_bud) & " €"
Cells(11, 5).Font.ColorIndex = 10
ElseIf moy_ibis_bud < moy_ibis_bud2 Then
Cells(11, 5) = "- " & Abs(diff_moy_ibis_bud) & " €"
Cells(11, 5).Font.ColorIndex = 3
ElseIf moy_ibis_bud = moy_ibis_bud2 Then
Cells(11, 5) = "/"
End If
If moy_ball > moy_ball2 Then 'baladins
Cells(11, 6) = "+ " & Abs(diff_moy_ball) & " €"
Cells(11, 6).Font.ColorIndex = 10
ElseIf moy_ball < moy_ball2 Then
Cells(11, 6) = "- " & Abs(diff_moy_ball) & " €"
Cells(11, 6).Font.ColorIndex = 3
ElseIf moy_ball = moy_ball2 Then
Cells(11, 6) = "/"
End If
If moy_apart > moy_apart2 Then 'appart hotel
Cells(11, 7) = "+ " & Abs(diff_moy_apart) & " €"
Cells(11, 7).Font.ColorIndex = 10
ElseIf moy_apart < moy_apart2 Then
Cells(11, 7) = "- " & Abs(diff_moy_apart) & " €"
Cells(11, 6).Font.ColorIndex = 3
ElseIf moy_apart = moy_apart2 Then
Cells(11, 7) = "/"
End If
If moy_adagio > moy_adagio2 Then 'adagio
Cells(11, 8) = "+ " & Abs(diff_moy_adagio) & " €"
Cells(11, 8).Font.ColorIndex = 10
ElseIf moy_adagio < moy_adagio2 Then
Cells(11, 8) = "- " & Abs(moy_adagio) & " €"
Cells(11, 6).Font.ColorIndex = 3
ElseIf moy_adagio = moy_adagio2 Then
Cells(11, 8) = "/"
End If
If moy_s_et_a > moy_s_et_a2 Then 's et a
Cells(11, 9) = "+ " & Abs(diff_moy_s_et_a) & " €"
Cells(11, 9).Font.ColorIndex = 10
ElseIf moy_s_et_a < moy_s_et_a2 Then
Cells(11, 9) = "- " & Abs(diff_moy_s_et_a) & " €"
Cells(11, 9).Font.ColorIndex = 3
ElseIf moy_s_et_a = moy_s_et_a2 Then
Cells(11, 9) = "/"
End If
MsgBox "Fait !"
End Sub
A voir également:
- Résultat incohérent avec excel en VBA
- Liste déroulante excel - Guide
- Mise en forme conditionnelle excel - Guide
- Word et excel gratuit - Guide
- Resultat foot - Télécharger - Vie quotidienne
- Déplacer colonne excel - Guide
3 réponses
Salut,
Deja il doit y a des erreurs dans ton prog (j'ai mis en gras 3 erreurs je pense)
(dsl si la mise en forme est pourrie, pas réussie à la garder)
If moy_apart > moy_apart2 Then 'appart hotel
Cells(11, 7) = "+ " & Abs(diff_moy_apart) & " €"
Cells(11, 7).Font.ColorIndex = 10
ElseIf moy_apart < moy_apart2 Then
Cells(11, 7) = "- " & Abs(diff_moy_apart) & " €"
Cells(11, 7).Font.ColorIndex = 3
ElseIf moy_apart = moy_apart2 Then
Cells(11, 7) = "/"
End If
If moy_adagio > moy_adagio2 Then 'adagio
Cells(11, 8) = "+ " & Abs(diff_moy_adagio) & " €"
Cells(11, 8).Font.ColorIndex = 10
ElseIf moy_adagio < moy_adagio2 Then
Cells(11, 8) = "- " & Abs(diff_moy_adagio) & " €"
Cells(11, 8).Font.ColorIndex = 3
ElseIf moy_adagio = moy_adagio2 Then
Cells(11, 8) = "/"
Deja il doit y a des erreurs dans ton prog (j'ai mis en gras 3 erreurs je pense)
(dsl si la mise en forme est pourrie, pas réussie à la garder)
If moy_apart > moy_apart2 Then 'appart hotel
Cells(11, 7) = "+ " & Abs(diff_moy_apart) & " €"
Cells(11, 7).Font.ColorIndex = 10
ElseIf moy_apart < moy_apart2 Then
Cells(11, 7) = "- " & Abs(diff_moy_apart) & " €"
Cells(11, 7).Font.ColorIndex = 3
ElseIf moy_apart = moy_apart2 Then
Cells(11, 7) = "/"
End If
If moy_adagio > moy_adagio2 Then 'adagio
Cells(11, 8) = "+ " & Abs(diff_moy_adagio) & " €"
Cells(11, 8).Font.ColorIndex = 10
ElseIf moy_adagio < moy_adagio2 Then
Cells(11, 8) = "- " & Abs(diff_moy_adagio) & " €"
Cells(11, 8).Font.ColorIndex = 3
ElseIf moy_adagio = moy_adagio2 Then
Cells(11, 8) = "/"
si je suis la logique de son programme, je trouve cela incohérent.
Et quand tu l'éxécutes, les cases 11.7 et 11.8 donnent à mon avis pas le bon résultat
Il compare B9-B10 et met résultat en B11 (je passe les onglets différents)
Idem pour colonne C-D-E-F et I
Pour colonne G et H (7 et 8), pourquoi le code serait-il différent ?
Et quand tu l'éxécutes, les cases 11.7 et 11.8 donnent à mon avis pas le bon résultat
Il compare B9-B10 et met résultat en B11 (je passe les onglets différents)
Idem pour colonne C-D-E-F et I
Pour colonne G et H (7 et 8), pourquoi le code serait-il différent ?
Je pense avoir trouvé ton soucis (je suis pas pro en vba)
Ton problème vient de la déclaration en single.
Les nombres à virgule de type single stockent une approximation d'un nombre réel
https://docs.microsoft.com/fr-fr/dotnet/visual-basic/programming-guide/language-features/data-types/troubleshooting-data-types
exemple :
Si tu convertis une valeur 8 en single, ca affiche 8
Si tu convertis 5.03 par contre , la valeur stockée en single est 5,03000020980834
Soit tu changes de type de variable soit tu formates tes valeurs de sortie pour n'avoir que les 2 derniers chiffres après la virgule
exemple : Cells(11, 2) = "+ " & Abs(arrondi(diff_moy_ibis_orleans;2)) & " €"
Regarde la partie de ton code des if pour apart et adago, je pense qu'il y a quand meme 3 erreurs (voir au dessus)
Je peux me tromper mais je trouverai ca + logique au vu des autres parties de code.
Ton problème vient de la déclaration en single.
Les nombres à virgule de type single stockent une approximation d'un nombre réel
https://docs.microsoft.com/fr-fr/dotnet/visual-basic/programming-guide/language-features/data-types/troubleshooting-data-types
exemple :
Si tu convertis une valeur 8 en single, ca affiche 8
Si tu convertis 5.03 par contre , la valeur stockée en single est 5,03000020980834
Soit tu changes de type de variable soit tu formates tes valeurs de sortie pour n'avoir que les 2 derniers chiffres après la virgule
exemple : Cells(11, 2) = "+ " & Abs(arrondi(diff_moy_ibis_orleans;2)) & " €"
Regarde la partie de ton code des if pour apart et adago, je pense qu'il y a quand meme 3 erreurs (voir au dessus)
Je peux me tromper mais je trouverai ca + logique au vu des autres parties de code.