Bonjour tous les expert en excel.
Qui peut m’aider à résoudre le problème de règle de 80 dans le code ci dessus.
Je veux que seul le 80 = quatre-vingts avec « s »
Mais le reste comme 80 000 = quatre-vingt mille sans « s »
Option Explicit
Public Function ChLettres(Montant As Double, Optional devise As Byte = 0, Optional Langue As Byte = 0) As String
Dim partieEntiere As Variant, partieDecimale As Integer
Dim texteDevise As String, texteCentimes As String, position As Byte
If (Montant < 0) Then Montant = Abs(Montant)
partieEntiere = Int(Montant)
position = InStr(1, Montant, « , »)
If (position < 1) Then position = InStr(1, Montant, « , »)
If (position > 0) Then
position = position + 1
If devise = 0 Or devise = 4 Then
partieDecimale = Left(Mid(Montant, position), 3)
Else
partieDecimale = Left(Mid(Montant, position), 2)
End If
Else
partieDecimale = 0
End If
If partieDecimale = 0 Then
If partieEntiere > 999999999999999# Then
ChLettres = « # TropGrand # »
Exit Function
End If
Else
If partieEntiere > 9999999999999# Then
ChLettres = « # TropGrand # »
Exit Function
End If
End If
Select Case devise
Case 0
If partieDecimale > 0 Then texteDevise = " virgule"
Case 1
texteDevise = " Euro"
If partieDecimale > 0 Then texteCentimes = " Cents"
Case 2
texteDevise = " Dollar"
If partieDecimale > 0 Then texteCentimes = " Cent"
Case 3
’ Pour Ariary, on met virgule avant décimales et ariary à la fin
If partieDecimale > 0 Then
texteDevise = " virgule"
texteCentimes = " Ariary"
Else
texteDevise = " Ariary"
texteCentimes = « »
End If
Case 4
texteDevise = " Dinar"
If partieDecimale = 1 Then texteCentimes = " Millime"
If partieDecimale > 1 Then texteCentimes = " Millimes"
End Select
If devise = 0 And partieEntiere = 0 Then texteDevise = « zéro » & texteDevise
If devise > 0 And partieEntiere = 0 Then texteDevise = « zéro » & texteDevise
Select Case devise
Case 1, 2, 4
If partieEntiere > 1 Then texteDevise = texteDevise & « s »
Case 3
If partieEntiere > 1 Then texteDevise = texteDevise
End Select
ChLettres = NbEnMille(CDbl(partieEntiere), Langue) & texteDevise & " " & NbEnCent(partieDecimale, Langue) & texteCentimes
End Function
Private Function NbEnMille(Nombre As Double, Langue As Byte) As String
Dim tabBloc As Variant: Dim numBloc As Byte
Dim nombreBloc As Integer, reste As Double, texteBloc As String
tabBloc = Array(« », « mille », « million », « milliard », « billion »)
numBloc = 0
If Nombre = 0 Then Exit Function
Do While Nombre > 0
reste = Int(Nombre / 1000)
nombreBloc = Nombre - (reste * 1000)
If nombreBloc > 0 Then
texteBloc = NbEnCent(CInt(nombreBloc), Langue)
Select Case numBloc
Case 0
' Rien à ajouter, c’est l’unité
Case 1
If nombreBloc = 1 Then
texteBloc = "mille"
Else
texteBloc = texteBloc & " mille"
End If
Case Else
If nombreBloc = 1 Then
texteBloc = "un " & tabBloc(numBloc)
Else
texteBloc = texteBloc & " " & tabBloc(numBloc) & "s"
End If
End Select
NbEnMille = texteBloc & " " & NbEnMille
End If
Nombre = reste
numBloc = numBloc + 1
Loop
NbEnMille = UCase(Left(NbEnMille, 1)) & Mid(NbEnMille, 2)
End Function
Private Function NbEnCent(Nombre As Integer, Langue As Byte) As String
Dim tabUnites As Variant
Dim NbCent As Byte, reste As Byte, texteReste As String
tabUnites = Array(« », « un », « deux », « trois », « quatre », « cinq », « six », « sept », « huit », « neuf », « dix »)
NbCent = Int(Nombre / 100): reste = Nombre - (NbCent * 100)
texteReste = NbEnDizaines(reste, Langue)
Select Case NbCent
Case 0
NbEnCent = texteReste
Case 1
If reste = 0 Then
NbEnCent = « cent »
Else
NbEnCent = « cent " & texteReste
End If
Case Else
If reste = 0 Then
NbEnCent = tabUnites(NbCent) & " cents »
Else
NbEnCent = tabUnites(NbCent) & " cent " & texteReste
End If
End Select
End Function
Private Function NbEnDizaines(Nombre As Byte, Langue As Byte) As String
Dim tabUnites As Variant, tabDizaines As Variant
Dim nbUnites As Byte, nbDizaines As Byte
Dim texteLiaison As String
tabUnites = Array(« », « un », « deux », « trois », « quatre », « cinq », « six », « sept », « huit », « neuf », « dix », « onze », « douze », « treize », « quatorze », « quinze », « seize », « dix-sept », « dix-huit », « dix-neuf »)
tabDizaines = Array(« », « », « vingt », « trente », « quarante », « cinquante », « soixante », « soixante », « quatre-vingt », « quatre-vingt »)
If Langue = 1 Then
tabDizaines(7) = « septante »
tabDizaines(9) = « nonante »
End If
If Langue = 2 Then tabDizaines(8) = « huitante »
nbDizaines = Int(Nombre / 10): nbUnites = Nombre - (nbDizaines * 10)
texteLiaison = « - »: If nbUnites = 1 Then texteLiaison = " et "
Select Case nbDizaines
Case 0
texteLiaison = « »
Case 1
nbUnites = nbUnites + 10
texteLiaison = « »
Case 7
If Langue = 0 Then nbUnites = nbUnites + 10
Case 8
If Langue <> 2 Then texteLiaison = « - »
Case 9
If Langue = 0 Then
nbUnites = nbUnites + 10
texteLiaison = « - »
End If
End Select
NbEnDizaines = tabDizaines(nbDizaines)
If Langue <> 2 And Nombre = 80 Then NbEnDizaines = tabDizaines(nbDizaines) & « s »
If (tabUnites(nbUnites) <> « ») Then NbEnDizaines = NbEnDizaines & texteLiaison & tabUnites(nbUnites)
End Fun