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
| Option Explicit
Private Sub Text1_KeyPress(KeyAscii As Integer)
Dim CiblE As String
If KeyAscii = 13 Then
KeyAscii = 0
CiblE = ExtratAdditionChaine(Text1.Text)
If CiblE <> "-999" Then MsgBox CiblE
End If
End Sub
Function ExtratAdditionChaine(DataChaine As String) As Single
On Error GoTo errfatale:
Dim i As Byte
Dim NombreConv As Single, Resultat As Single
'DataChaine = "12,3azerty23,5 67" ********* Resultat = 102.8 ********
DataChaine = Replace(DataChaine, ",", ".")
'Pour gérer deux nombres qui se suivent: remplacement des espaces par un caractère Alpha
DataChaine = Replace(DataChaine, " ", "x")
For i = 1 To Len(DataChaine)
' MsgBox "conversion pas à pas ok "
If IsNumeric(Mid(DataChaine, i, 1)) Then
NombreConv = Val(Mid(DataChaine, i, Len(DataChaine) - i + 1))
Resultat = Resultat + NombreConv
i = i + Len(Str(NombreConv)) - 1
End If
Next
ExtratAdditionChaine = Resultat
'MsgBox "fin ExtratAdditionChaine ok "
Exit Function
errfatale:
MsgBox Err.Number & vbCrLf & Err.Description
Err.Clear
ExtratAdditionChaine = -999
End Function |