Bonjour à tous,

Voilà mon problème : je cherche à réaliser une fonction qui me permette de formater une textbox en format Date, car ayant une multitude de formulaires avec des saisies de date, cela devient lourd de réécrire le code pour chaque textbox.

J'ai donc réussi pour le moment à réaliser la chose de cette manière (le code fonctionne) :

- Code de ma fonction (dans un module nommé MaFonction) :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
Public DateSaisie As Control
 
Public Function FormaterDate()
 
If IsNumeric(DateSaisie) Then
 
    If Len(DateSaisie) = 1 Then
 
    DateSaisie = "0" & DateSaisie & "/" & Format(Date, "mm") & "/" & Format(Date, "yyyy")
 
        ElseIf Len(DateSaisie) = 2 Then
        DateSaisie = DateSaisie & "/" & Format(Date, "mm") & "/" & Format(Date, "yyyy")
 
        ElseIf Len(DateSaisie) = 4 Then
        DateSaisie = Left(DateSaisie, 2) & "/" & Right(DateSaisie, 2) & "/" & Format(Date, "yyyy")
 
        ElseIf Len(DateSaisie) = 6 Then
        DateSaisie = Left(DateSaisie, 2) & "/" & Mid(DateSaisie, 3, 2) & "/" & Right(DateSaisie, 2)
 
        ElseIf Len(DateSaisie) = 8 Then
        DateSaisie = Left(DateSaisie, 2) & "/" & Mid(DateSaisie, 3, 2) & "/" & Right(DateSaisie, 4)
 
    End If
 
End If
 
End Function
- Code de mon UserForm1 :
Code : Sélectionner tout - Visualiser dans une fenêtre à part
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
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
Private Sub UserForm_Initialize()
 
TextBox1.Value = Date
Label2.Caption = WorksheetFunction.Proper(Format(TextBox1.Value, "dddd d mmmm yyyy"))
TextBox1.SelStart = 0
TextBox1.SelLength = Len(TextBox1.Text)
 
End Sub
 
Private Sub TextBox1_Enter()
 
        TextBox1.Tag = TextBox1
 
End Sub
 
Private Sub TextBox1_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
 
Set DateSaisie = TextBox1
 
MaFonction.FormaterDate
 
If Not IsDate(DateSaisie) Then
 
    MsgBox "Format de date incorrect", vbCritical + vbOKOnly, "Erreur de saisie"
    DateSaisie = DateSaisie.Tag
    Cancel = True
    DateSaisie.SelStart = 0
    DateSaisie.SelLength = Len(DateSaisie.Text)
 
End If
 
End Sub
 
Private Sub TextBox1_AfterUpdate()
 
TextBox1.Value = Format(TextBox1.Value, "dd/mm/yyyy")
Label2.Caption = WorksheetFunction.Proper(Format(TextBox1.Value, "dddd d mmmm yyyy"))
 
End Sub
Private Sub CommandButton1_Click()
 
Range("C4") = TextBox1
Range("C6") = TextBox2
 
Unload Me
 
End Sub
 
Private Sub CommandButton2_Click()
 
Unload Me
 
End Sub
Maintenant, ce que je souhaiterais, c'est que ce bout de code vienne à la suite de ma fonction et non pas dans l'évènement TextBox1_BeforeUpdate, car cela m'oblige à réécrire le code pour chaque textbox à formater :

Code : Sélectionner tout - Visualiser dans une fenêtre à part
1
2
3
4
5
6
7
8
9
If Not IsDate(DateSaisie) Then
 
    MsgBox "Format de date incorrect", vbCritical + vbOKOnly, "Erreur de saisie"
    DateSaisie = DateSaisie.Tag
    Cancel = True
    DateSaisie.SelStart = 0
    DateSaisie.SelLength = Len(DateSaisie.Text)
 
End If
Or, si j'ajoute ce code dans ma fonction, je n'obtiens pas l'effet escompté. C'est à dire que les propriétés "Cancel", "SelStart" et "SelLength" ne s'appliquent pas (alors que la msgbox s'affiche et le tag fonctionne), et je perds le focus de ma Textbox1 en cas d'erreur de saisie

Ma question est donc la suivante : Est-il possible d'intégrer ce bout de code à ma fonction ? Ou suis-je obligé de le laisser dans l'évènement BeforeUpdate ?

J'espère que je me suis fait comprendre et merci d'avance à tous ceux qui liront ce post.

Japeth