Code VBA pour écrire les nombres en lettres
Bonjour à tous
J ai un fichier. Je voudrais avoir dans les feuilles Factură nouă (dans la cellule H29) et Export chitanță (dans les cellules - E16 et E 44 - le nombre introduit dans la cellule D3) quand j écris un nombre - sa transcription en lettres.
J ai un code, mais je ne sais pas comment l introduire. Vous pourriez m aider?
Voilà le code:
Dim intreg
intreg = Int(r.Value)
Dim cifre As Variant
Dim cifrefeminin As Variant
Dim zecedouazeci As Variant
cifre = Array("zero", "un", "doi", "trei", "patru", "cinci", "sase", "sapte", "opt", "noua")
cifrefeminin = Array("zero", "o", "doua", "trei", "patru", "cinci", "sase", "sapte", "opt", "noua")
zecedouazeci = Array("zece", "unsprezece", "doisprezece", "treisprezece", "paisprezece" _
, "cincisprezece", "saisprezece", "saptesprezece", "optsprezece", "nouasprezece")
Dim rezultat As String
rezultat = "zero"
If (intreg >= 100) Then
Dim sute As Integer
sute = Int(intreg / 100)
If (sute <> 1) Then
rezultat = cifrefeminin(sute Mod 10) & " sute"
Else
rezultat = "o suta"
End If
intreg = intreg Mod 100
Else
rezultat = ""
End If
If (intreg >= 10 And intreg <= 19) Then
rezultat = rezultat & " " & zecedouazeci(intreg - 10)
Else
If (intreg >= 10) Then
Dim zeci As Integer
zeci = Int(intreg / 10)
rezultat = rezultat & " " & cifrefeminin(zeci Mod 10) & "zeci"
If (intreg Mod 10 > 0) Then
rezultat = rezultat & " si"
End If
intreg = intreg Mod 10
End If
If (intreg > 0) Then
rezultat = rezultat & " " & cifre(intreg Mod 10)
End If
End If
If (r.Value < 1) Then
rezultat = "zero"
End If
NumarInLitere = Trim(rezultat) & " RON si " & Int(100 * (r.Value - Int(r.Value))) & " bani"
End Function
J ai trouvé encore un code:
'****************
' Main Function *
'****************
Function SpellNumber(ByVal MyNumber)
Dim Dollars, Cents, Temp
Dim DecimalPlace, Count
ReDim Place(9) As String
Place(2) = "mii"
Place(3) = "milioane"
Place(4) = "miliarde"
Place(5) = "trilioane"
' String representation of amount.
MyNumber = Trim(Str(MyNumber))
' Position of decimal place 0 if none.
DecimalPlace = InStr(MyNumber, ".")
' Convert cents and set MyNumber to dollar amount.
If DecimalPlace > 0 Then
Cents = GetTens(Left(Mid(MyNumber, DecimalPlace + 1) & "00", 2))
MyNumber = Trim(Left(MyNumber, DecimalPlace - 1))
End If
Count = 1
Do While MyNumber <> ""
Temp = GetHundreds(Right(MyNumber, 3))
If Temp <> "" Then Dollars = Temp & Place(Count) & Dollars
If Len(MyNumber) > 3 Then
MyNumber = Left(MyNumber, Len(MyNumber) - 3)
Else
MyNumber = ""
End If
Count = Count + 1
Loop
Select Case Dollars
Case ""
Dollars = "zerolei"
Case "One"
Dollars = "unleu"
Case Else
Dollars = Dollars & "lei"
End Select
Select Case Cents
Case ""
Cents = "şizerobani"
Case "One"
Cents = "şiunban"
Case Else
Cents = "şi" & Cents & "bani"
End Select
SpellNumber = Dollars & Cents
End Function
'*******************************************
' Converts a number from 100-999 into text *
'*******************************************
Function GetHundreds(ByVal MyNumber)
Dim Result As String
If Val(MyNumber) = 0 Then Exit Function
MyNumber = Right("000" & MyNumber, 3)
' Convert the hundreds place.
If Mid(MyNumber, 1, 1) <> "0" Then
Result = GetDigit(Mid(MyNumber, 1, 1)) & "sute"
End If
' Convert the tens and ones place.
If Mid(MyNumber, 2, 1) <> "0" Then
Result = Result & GetTens(Mid(MyNumber, 2))
Else
Result = Result & GetDigit(Mid(MyNumber, 3))
End If
GetHundreds = Result
End Function
'*********************************************
' Converts a number from 10 to 99 into text. *
'*********************************************
Function GetTens(TensText)
Dim Result As String
Result = "" ' Null out the temporary function value.
If Val(Left(TensText, 1)) = 1 Then ' If value between 10-19...
Select Case Val(TensText)
Case 10: Result = "zece"
Case 11: Result = "unsprezece"
Case 12: Result = "douãsprezece"
Case 13: Result = "treisprezece"
Case 14: Result = "patrusprezece"
Case 15: Result = "cincisprezece"
Case 16: Result = "şaisprezece"
Case 17: Result = "şaptesprezece"
Case 18: Result = "optsprezece"
Case 19: Result = "nouãsprezece"
Case Else
End Select
Else ' If value between 20-99...
Select Case Val(Left(TensText, 1))
Case 2: Result = "douãzecişi"
Case 3: Result = "treizecişi"
Case 4: Result = "patruzecişi"
Case 5: Result = "cincizecişi"
Case 6: Result = "şaizecişi"
Case 7: Result = "şaptezecişi"
Case 8: Result = "optzecişi"
Case 9: Result = "nouãzecişi"
Case Else
End Select
Result = Result & GetDigit(Right(TensText, 1)) ' Retrieve ones place.
End If
GetTens = Result
End Function
'*******************************************
' Converts a number from 1 to 9 into text. *
'*******************************************
Function GetDigit(Digit)
Select Case Val(Digit)
Case 1: GetDigit = "unu"
Case 2: GetDigit = "doi"
Case 3: GetDigit = "trei"
Case 4: GetDigit = "patru"
Case 5: GetDigit = "cinci"
Case 6: GetDigit = "şase"
Case 7: GetDigit = "şapte"
Case 8: GetDigit = "opt"
Case 9: GetDigit = "nouã"
Case Else: GetDigit = ""
End Select
End Function
Bonjour,
déjà résolu sur ce forum
https://forum.excel-pratique.com/cours-astuces/transformer-nombre-en-lettres-t28766.html
Bonjour,
Avec la deuxième fonction (SpellNumber)
mais il manque des espaces
Oui, je vois, merci pour ton aide.
Que dois-je faire pour exister des espaces entre les mots ? Je dois modifier le code? Et dans le cas où il n y a rien après la virgule (aucune décimale), est-il possible d effacer le mot BANI (unite de la monnaie - c est-a-dire CENTS) ?
bernard22 a écrit :Bonjour,
déjà résolu sur ce forum
https://forum.excel-pratique.com/cours-astuces/transformer-nombre-en-lettres-t28766.html
Salut, Bernard!
Oui, mais c est pour un autre pays.
Re,
A tester,
Après, je ne parle pas autre langue que le Français et modérement l'Anglais
Merci beaucoup pour ton aide. Il y a encore des espaces.
Bonne soirée!