XL 2016 Problème format nombre à la sortie Textbox

barbin

XLDnaute Junior
Bonjour à tous.
a la sortie de ma textbox après avoir saisie 10.50, celle-ci m'affiche 0,45.
Voici le code de ma textbox :
Private Sub TextBox15_Exit(ByVal Cancel As MSForms.ReturnBoolean)
TextBox15.Value = Format(TextBox15.Value, "# ##0.00")
End Sub

Private Sub TextBox15_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If InStr("1234567890.-", Chr(KeyAscii)) = 0 Or TextBox15.SelStart > 0 And Chr(KeyAscii) = "-" _
Or InStr(TextBox15.Value, ".") <> 0 And Chr(KeyAscii) = "." Then KeyAscii = 0: Beep
End Sub

Merci de m'aider svp.
 

Dranreb

XLDnaute Barbatruc
Bonjour.
Chez moi Format("10.50", "# ##0.00") vaut aussi "0,45"
Il manque à mon avis la seule chose importante dans la _KeyPressed :
VB:
Private Sub TextBox15_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
   If Chr$(KeyAscii) = "." Then KeyAscii = Asc(",")
   End Sub
 

patricktoulon

XLDnaute Barbatruc
BONJOUR
tu peux taper ce que tu veux dans un textbox
si tu l'envoie comme tel dans une cellule
OU!!
si tu envoie format(Textbox15,"....")
résultat
tu injecte en string et donc force excel a évaluer dans un format

par contre si tu envoie Val(Textbox15) et!!!!!! que tu format ta cellule la oui tu aura le bon format
exemple
cells(1,1)=val(textbox15)
SI!!! pour toi il est important d'avoir le séparateur de millier dans le textbox
du dois reformater avant d'injecter

exemple
cells(1,1)=val(replace(Textbox15," ",""))

;)
 

patricktoulon

XLDnaute Barbatruc
re
tiens je l'ai retrouvé
formater un textbox +suffixe monétaire
on gère les touches del et suppr en prime
il n'est pas possible d'inserer entre deux
2 décimales après la virgule
VB:
Private Sub TextBox15_KeyDown(ByVal KeyCode As MSForms.ReturnInteger, ByVal Shift As Integer)
    With TextBox15
         x = Trim(Replace(.Value, " €", ""))
        Select Case KeyCode
        Case 96 To 105
        If Right(x, 3) Like ",*" Then KeyCode = 0
          x = x & Chr(KeyCode + IIf(KeyCode < 96, 32, -48))
        Case 110, 188: If Not x Like "*,*" Then x = x & ","
        Case 8: x = Left(x, Len(x) - IIf(x <> "", 1, 0))
        Case 46: x = Left(x, .SelStart)
        Case Else: KeyCode = 0
        End Select
        x = IIf(Not x Like "*,*", Format(x, Right("### ### ### ### ### ### ### ###", Len(x))), x)
        .Value = x
        If .Value <> "" Then .Value = .Value & " €"
        .SelStart = Len(x)
    End With
    KeyCode = 0
End Sub
 

dg62

XLDnaute Barbatruc
Bonjour le fil
j'ai testé en créant un textbox et j'ai essayé cela
VB:
Private Sub TextBox15_Exit(ByVal Cancel As MSForms.ReturnBoolean)
MsgBox Format(TextBox15.Value, "# ##0.00")
End Sub

et j'ai 10.5 en retour. c'est pour cela que j'avais dit que cela était conforme à la saisie.
 

jmfmarques

XLDnaute Accro
Bonjour à tous
Une textbox contient du texte (type string)
Si le séparateur décimal utilisé est le ".", on convertit en numérique, avant formatage
VB:
MsgBox Format(Val(textbox1.value), "# ##0.00") ' affichera en utilisant le séparateur décimal de la version.
 

Discussions similaires

Réponses
15
Affichages
488
Réponses
20
Affichages
1 K