Boostez vos compétences Excel avec notre communauté !
Rejoignez Excel Downloads, le rendez-vous des passionnés où l'entraide fait la force. Apprenez, échangez, progressez – et tout ça gratuitement ! 👉 Inscrivez-vous maintenant !
Function DateLettreEnNbre(Cellule As Range) As String 'David84 http://www.excel-downloads.com/forum/148188-expressions-regulieres-patterns-pour-regexp-11.html#post1092727
Dim oRegExp, Match, Matches, Mois, Unités, Dizaines, Dizaine, Centaines, Milliers, i As Integer, MaChaine As String, ChaineInit As String, _
MonPattern As String, j As Byte, Item, jour, an, testUnit As Boolean, testMill As Boolean, testCent As Boolean, testDiz As Boolean, testDizs As Boolean, _
temp1 As Byte, temp2 As Byte, temp3 As Byte, temp4 As Byte, temp5 As Byte, antemp
If Cellule = "" Then Exit Function
MaChaine = LCase(Cellule.Value)
MaChaine = Replace(Replace(" " & MaChaine & " ", "premier", "un"), " mil ", " mille ")
Set oRegExp = CreateObject("vbscript.regexp")
With oRegExp
.Global = True
.IgnoreCase = True
Unités = Array("un", "deux", "trois", "quatre", "cinq", "six", "sept", "huit", "neuf")
Dizaine = Array("onze", "douze", "treize", "quatorze", "quinze", "seize", "dix sept", "dix huit", "dix neuf")
Dizaines = Array("dix", "vingt", "trente", "quarante", "cinquante", "soixante", "soixante dix", "quatre vingt", "quatre vingt dix")
Mois = Array(" janvier ", " février ", " mars ", " avril ", " mai ", " juin ", " juillet ", " août ", " septembre ", " octobre ", " novembre ", " décembre ")
Centaines = Array("cent", "deux cent", "trois cent", "quatre cent", "cinq cent", "six cent", "sept cent", "huit cent", "neuf cent")
Milliers = Array("mille", "deux mille")
'traitement du mois
For i = LBound(Mois) To UBound(Mois)
.Pattern = Mois(i)
Set Matches = .Execute(MaChaine)
If .Test(MaChaine) = True Then
For j = 0 To Matches.Count - 1
MaChaine = Replace(MaChaine, Matches.Item(j), " /" & Format(i + 1, "00") & "/ ")
Next j
End If
Next i
MaChaine = Replace(Replace(MaChaine, "-", " "), " et ", " ")
ChaineInit = MaChaine
'Traitement du jour
testDiz = False And testDizs = False And testUnit = False
jour = Left(MaChaine, InStr(1, MaChaine, "/") - 1)
For i = UBound(Dizaine) To LBound(Dizaine) Step -1
.Pattern = Dizaine(i)
Set Matches = .Execute(jour)
If .Test(jour) = True Then
For j = 0 To Matches.Count - 1
temp1 = i + 11: testDiz = True
Next j
End If
If testDiz = True Then Exit For
Next i
If testDiz = False Then
For i = UBound(Dizaines) To LBound(Dizaines) Step -1
.Pattern = Dizaines(i)
Set Matches = .Execute(jour)
If .Test(jour) = True Then
For j = 0 To Matches.Count - 1
temp2 = (i + 1) * 10: testDizs = True
Next j
End If
If testDizs = True Then Exit For
Next i
For i = UBound(Unités) To LBound(Unités) Step -1
.Pattern = Unités(i)
Set Matches = .Execute(jour)
If .Test(jour) = True Then
For j = 0 To Matches.Count - 1
temp3 = i + 1: testUnit = True
Next j
End If
If testUnit = True Then Exit For
Next i
End If
If testDiz = False And testDizs = False And testUnit = True Then MaChaine = Replace(MaChaine, jour, temp3, , 1): GoTo TraitementAnnée
If testDiz = True And testDizs = False And testUnit = False Then MaChaine = Replace(MaChaine, jour, temp1, , 1): GoTo TraitementAnnée
If testDiz = False And testDizs = True And testUnit = False Then MaChaine = Replace(MaChaine, jour, temp2, , 1): GoTo TraitementAnnée
If testDiz = False And testDizs = True And testUnit = True Then MaChaine = Replace(MaChaine, jour, Left(temp2, 1) & temp3, , 1): GoTo TraitementAnnée
'traitement de l'année
TraitementAnnée:
testMill = False
testCent = False
testDiz = False
testDizs = False
testUnit = False
temp1 = 0
temp2 = 0
temp3 = 0
temp4 = 0
temp5 = 0
an = Trim(Right(MaChaine, Len(MaChaine) - InStrRev(MaChaine, "/")))
antemp = an
For i = UBound(Milliers) To LBound(Milliers) Step -1
.Pattern = Milliers(i)
Set Matches = .Execute(antemp)
If .Test(antemp) = True Then
For j = 0 To Matches.Count - 1
temp1 = i + 1: testMill = True
antemp = Trim(Replace(antemp, Milliers(i), ""))
Next j
End If
If testMill = True Then Exit For
Next i
For i = UBound(Centaines) To LBound(Centaines) Step -1
.Pattern = Centaines(i)
Set Matches = .Execute(antemp)
If .Test(antemp) = True Then
For j = 0 To Matches.Count - 1
temp2 = i + 1: testCent = True
antemp = Trim(Replace(antemp, Centaines(i), ""))
Next j
End If
If testCent = True Then Exit For
Next i
For i = LBound(Dizaine) To UBound(Dizaine)
.Pattern = Dizaine(i)
Set Matches = .Execute(antemp)
If .Test(antemp) = True Then
For j = 0 To Matches.Count - 1
temp4 = i + 11: testDiz = True
antemp = Trim(Replace(antemp, Dizaine(i), ""))
Next j
End If
If testDiz = True Then Exit For
Next i
For i = UBound(Dizaines) To LBound(Dizaines) Step -1
.Pattern = Dizaines(i)
Set Matches = .Execute(antemp)
If .Test(antemp) = True Then
For j = 0 To Matches.Count - 1
temp3 = i + 1: testDizs = True
antemp = Trim(Replace(antemp, Dizaines(i), ""))
Next j
End If
If testDizs = True Then Exit For
Next i
For i = LBound(Unités) To UBound(Unités)
.Pattern = Unités(i)
Set Matches = .Execute(antemp)
If .Test(antemp) = True Then
For j = 0 To Matches.Count - 1
temp5 = i + 1: testUnit = True
antemp = Trim(Replace(antemp, Unités(i), ""))
Next j
End If
If testUnit = True Then Exit For
Next i
If testMill = True And testCent = True And testDiz = False And testDizs = False And testUnit = True Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & 0 & temp5): GoTo Fin
If testMill = True And testCent = True And testDiz = True And testDizs = False And testUnit = True Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & 0 & temp5): GoTo Fin
If testMill = True And testCent = True And testDiz = False And testDizs = False And testUnit = False Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & 0 & 0): GoTo Fin
If testMill = True And testCent = True And testDiz = False And testDizs = True And testUnit = False Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & temp3 & temp4): GoTo Fin
If testMill = True And testCent = True And testDiz = True And testDizs = False And testUnit = False Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & temp4): GoTo Fin
If testMill = True And testCent = True And testDiz = False And testDizs = True And testUnit = True Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & temp3 & temp5): GoTo Fin 'vingt et un et ...
If testMill = True And testCent = True And testDiz = True And testDizs = True And testUnit = False Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & temp3 + Left(temp4, 1) & Right(temp4, 1)): GoTo Fin 'soixante et onze ...
If testMill = True And testCent = False And testDiz = False And testDizs = False And testUnit = False Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & temp3 & temp4): GoTo Fin 'deux mille ...
If testMill = True And testCent = False And testDiz = False And testDizs = False And testUnit = True Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & temp3 & temp5): GoTo Fin 'deux mille un...
If testMill = True And testCent = False And testDiz = False And testDizs = True And testUnit = False Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & temp3 & temp4): GoTo Fin 'deux mille dix...
If testMill = True And testCent = False And testDiz = True And testDizs = False And testUnit = False Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & temp4): GoTo Fin 'deux mille onze...
If testMill = True And testCent = False And testDiz = False And testDizs = True And testUnit = True Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & temp3 & temp5): GoTo Fin 'deux mille ving et un...
If testMill = True And testCent = False And testDiz = True And testDizs = True And testUnit = False Then MaChaine = Replace(MaChaine, an, temp1 & temp2 & temp3 + Left(temp4, 1) & Right(temp4, 1)): GoTo Fin 'deux mille soixante et onze...
Fin:
MaChaine = Trim(Replace(MaChaine, " ", ""))
If Len(MaChaine) = 9 Then MaChaine = 0 & MaChaine
.Pattern = "((0?[1-9]|[12]\d|3[01])[ /-](0?[13578]|1[02])|(0[1-9]|[12]\d|30)[ /-](0?[469]|11))[ /-]((1\d|2\d)\d\d)|(0[1-9]|1\d|2[0-8])[ /-]0?2[ /-]((1\d|2\d)\d\d)|29[ /-]0?2[ /-]((1[01345789]|2[1235679])0[48]|(1\d|2\d)([13579][26]|[2468][048])|(1[2604]|28)0[048])" '1000 à 2999
If .Test(MaChaine) = True Then DateLettreEnNbre = Format(MaChaine, "dd/mm/yyyy") Else DateLettreEnNbre = "Date non valide"
Set oRegExp = Nothing
Set Matches = Nothing
Set Item = Nothing
End With
End Function
permettant de retranscrire l'année chiffrée en année en lettres (cette fonction n'est utilisée que pour information et n'est pas liée à la fonction.
DateLettreEnNbre
pour les mois de septembre à décembre10 7bre 1895
2 Vendémiaire An1
que l'on retrouve dans les registres lors du début de l'utilisation de ce calendrier (donc s'il y a des erreurs dans le traitement de ce format, c'est de ma faute😱)*.2 Vendémiaire 1792
par RegExp.1er vendémiaire 1792
sont maintenant correctement traitées (la fonction ramène 20/09/1921).essai un-vingt septembre mille neuf cent vingt et unxxxxxessai deuxvingt septembre mille neuf cent vingt et unxx
ramène 6 fois la date 01/09/1794.les 15 fructidor an 2 et 15 fructidor an II et 1er septembre 1794 et premier septembre mille sept cent quatre vingt quatorze et 1-9-1794 et 1er sept. 1794
peut donner par exemple01/09/1794
oulundi 01 septembre 1794
Pour cela, vous n'avez qu'à préciser dans le 2ème argument (optionnel) de la fonction le format voulu.1794/09/01
ramènera 31/08/2011 tandis quetrente et un aout deux mille onze
ramènera 31/08/0011.31 8 11
Merci C@thy, c'est bien la première fois que j'entends une femme me dire qu'elle a tout ce qu'il faut et même plus qu'espéré.😱Carrément génial, maintenant on a tout ce qu'il faut, et même plus qu'espéré.
- Traitement des années comprises entre 1 et 99 : ajout d'un 2ème argument optionnel permettant ou non le type de traitement par défaut utilisé par Excel.deux mars 0=>02/03/2000
placée en B1:premier janvier un
placée en B1:premier janvier trente
...mais la possibilité d'intégrer directement à la fonction un format particulier peut également être un "plus" (qu'en pensez-vous ?)...
A priori, vu le type de validation (matricielle) de cette fonction, je pense que le plus simple est l'idée du "?" que tu as suggérée et que l'on peut directement appliquer au format traité dans la fonction.Je suis d'accord avec toi sur ce point. Ça serait effectivement un plus.
Maintenant la question se place au niveau de la faisabilité. Je ne sais pas ce qui est possible ou ce que est le plus facile pour toi.
Ben en fait, l'utilisateur a maintenant le choix : soit traiter les années entre 1 et 99 comme telle, soit les traiter comme tu le dis.- Traiter les années de 1 à 29 comme 2001 à 2029 et celles de 30 à 99 comme 1930 à 1999
C'est ce que j'ai proposé dans le dernier fichier en utilisant le ? (mais c'est un exemple).- Mais en ajoutant le ? pour alerter le lecteur, (ou mettre l'année entre guillemets ou formatage particulier en fonction de ce qui est possible pour toi)
Pas besoin de mettre "les mains dans le cambouis". J'ai surtout besoin de retour de personnes qui voient les choses d'un autre oeil (à force d'être "le nez dans le guidon", on a parfois du mal à prendre un peu de recul). Ta participation est donc grandement appréciée.Il est certain que j'interviens ici pour seulement donner des idées, car je suis bien incapable de mettre les mains dans le "cambouis".
Mais à ce moment-là, tu te prives de la possibilité de ramener une date entre l'an 1 et 99 et je pensais personnellement que cette possibilité devait être proposée le cas échéant à l'utilisateur.L'idée était de reprendre ta version précédente avec le ? :
trente et un janvier 01
ou
trente et un janvier un
donnerait :
31/01/2001?
plutôt que :
31/01/0001?
lorsque le paramètre 1 n'est pas précisé.
We use cookies and similar technologies for the following purposes:
Est ce que vous acceptez les cookies et ces technologies?
We use cookies and similar technologies for the following purposes:
Est ce que vous acceptez les cookies et ces technologies?