OVH Cloud OVH Cloud

Encore format date dans TextBox

3 réponses
Avatar
David
Bonjour à tous

Je n'arrive pas à résoudre mon problème.

Voici 3 codes pour la saisie d'une date dans un TextBox. Cela fonctionne
parfaitement.

Grace à cette proc "Private Sub CommandButton1_Click()", la valeur de
TextBox3 est envoyée vers Feuil1 mais pas au bon format (mm/dd/yyyy)

Merci de votre aide

David

Sub TextBox3_AfterUpdate()
Dim jour As Integer, mois As Integer, annee As Integer
Dim JourMax As Integer
If Len(DateEntree) = 0 Then Exit Sub
jour = Day(Date)
mois = Month(Date)
annee = Year(Date)
JourMax = 31
Select Case Len(DateEntree)
Case Is = 1, Is = 2
jour = DateEntree
Case Is = 3, Is = 4
jour = Left(DateEntree, 2)
mois = Right(DateEntree, Len(DateEntree) - 2)
Case Is > 4
jour = Left(DateEntree, 2)
mois = Mid(DateEntree, 3, 2)
annee = Right(DateEntree, Len(DateEntree) - 4)
End Select
If mois = 0 Then mois = 1
If mois > 12 Then mois = 12
If mois <> 12 Then JourMax = Day(DateAdd("d", -1, "01/" & mois + 1 & "/" &
annee))
If jour <= 0 Then jour = 1
If jour > JourMax Then jour = JourMax
TextBox3.Value = Format(jour & "/" & mois & "/" & annee, "dd/mm/yyyy")
End Sub

Private Sub TextBox3_Change()
Dim i As Byte, car As String
DateEntree = ""
For i = 1 To Len(TextBox3.Value)
If Mid(TextBox3.Value, i, 1) <> "/" Then DateEntree = DateEntree &
Mid(TextBox3.Value, i, 1)
Next i
End Sub

Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii <> 8 And KeyAscii <> 10 And KeyAscii <> 13 And (KeyAscii < 48 Or
KeyAscii > 57) Then
KeyAscii = 0
Exit Sub
Else
If Len(TextBox3.Text) = 2 Or Len(TextBox3.Text) = 5 Then TextBox3.Text
= TextBox3.Text + "/"
End If
End Sub

Private Sub TextBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
TextBox3.Value = Format(Date, "dd/mm/yyyy")
End Sub

Private Sub CommandButton1_Click()
Dim L2 As Integer
L2 = Sheets("Feuil1").Range("A65536").End(xlUp).Row + 1
With Sheets("Feuil1")
.Range("C" & L2).Value = TextBox3.Value
End With
End Sub

3 réponses

Avatar
Patrick Fredin
Bonjour,

Il faut convertir le contenu de la TextBox en date (comme le nom l'indique,
une TextBox contient du texte et non une valeur).

Donc voici comment le faire :

With Sheets("Feuil1")
.Range("C" & L2).Value = CDate(TextBox3.Value)
End With

--
Patrick

"David" <(Pas de Spam) wrote in message
news:
Bonjour à tous

Je n'arrive pas à résoudre mon problème.

Voici 3 codes pour la saisie d'une date dans un TextBox. Cela fonctionne
parfaitement.

Grace à cette proc "Private Sub CommandButton1_Click()", la valeur de
TextBox3 est envoyée vers Feuil1 mais pas au bon format (mm/dd/yyyy)

Merci de votre aide

David

Sub TextBox3_AfterUpdate()
Dim jour As Integer, mois As Integer, annee As Integer
Dim JourMax As Integer
If Len(DateEntree) = 0 Then Exit Sub
jour = Day(Date)
mois = Month(Date)
annee = Year(Date)
JourMax = 31
Select Case Len(DateEntree)
Case Is = 1, Is = 2
jour = DateEntree
Case Is = 3, Is = 4
jour = Left(DateEntree, 2)
mois = Right(DateEntree, Len(DateEntree) - 2)
Case Is > 4
jour = Left(DateEntree, 2)
mois = Mid(DateEntree, 3, 2)
annee = Right(DateEntree, Len(DateEntree) - 4)
End Select
If mois = 0 Then mois = 1
If mois > 12 Then mois = 12
If mois <> 12 Then JourMax = Day(DateAdd("d", -1, "01/" & mois + 1 & "/" &
annee))
If jour <= 0 Then jour = 1
If jour > JourMax Then jour = JourMax
TextBox3.Value = Format(jour & "/" & mois & "/" & annee, "dd/mm/yyyy")
End Sub

Private Sub TextBox3_Change()
Dim i As Byte, car As String
DateEntree = ""
For i = 1 To Len(TextBox3.Value)
If Mid(TextBox3.Value, i, 1) <> "/" Then DateEntree = DateEntree &
Mid(TextBox3.Value, i, 1)
Next i
End Sub

Private Sub TextBox3_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
If KeyAscii <> 8 And KeyAscii <> 10 And KeyAscii <> 13 And (KeyAscii < 48
Or
KeyAscii > 57) Then
KeyAscii = 0
Exit Sub
Else
If Len(TextBox3.Text) = 2 Or Len(TextBox3.Text) = 5 Then TextBox3.Text
= TextBox3.Text + "/"
End If
End Sub

Private Sub TextBox3_DblClick(ByVal Cancel As MSForms.ReturnBoolean)
TextBox3.Value = Format(Date, "dd/mm/yyyy")
End Sub

Private Sub CommandButton1_Click()
Dim L2 As Integer
L2 = Sheets("Feuil1").Range("A65536").End(xlUp).Row + 1
With Sheets("Feuil1")
.Range("C" & L2).Value = TextBox3.Value
End With
End Sub




Avatar
David
Bonsoir Patrick

Merci pour cette réponse très rapide.

Cela fonctionne très bien à condition que la cellule ne soit pas vide.

Je pensais donc mettre

if si vide
else
if si non vide

ou existe t'il une autre solution ?

Merci de ton aide

David

PS : Désolé pour l'E.mail erreur de manip
Avatar
Patrick Fredin
Bonsoir,

A priori, je ne vois pas pourquoi si la cellule n'est pas vide il y aurait
un problème. Enlève peut-être le ".Value".

--
Patrick

"David" <(Pas de Spam) wrote in message
news:
Bonsoir Patrick

Merci pour cette réponse très rapide.

Cela fonctionne très bien à condition que la cellule ne soit pas vide.

Je pensais donc mettre

if si vide
else
if si non vide

ou existe t'il une autre solution ?

Merci de ton aide

David

PS : Désolé pour l'E.mail erreur de manip