XL 2016 Saisie date du mois sans textBox

KTM

XLDnaute Impliqué
Salut Forum
Jai une textbox 1 qui contient un mois donné.
je saisi des dates dans la textbox 2
Comment parametrer cette textbox2 afin que la saisie soit limitée aux dates du mois dans textbox1 ?
Merci
 

job75

XLDnaute Barbatruc
Bonjour KTM, mapomme,
Le mois ne suffit pas, il faut aussi l'année...
Rien n'empêche de mettre l'année avec le mois, exemple "Août 2019" ou "8/19" dans TextBox1.

La macro si les 2 TextBoxes sont dans une feuille de calcul :
VB:
Private Sub TextBox2_LostFocus()
If Not IsDate("1/" & TextBox1) Then TextBox1 = "": TextBox2 = "": Exit Sub
If Not IsDate(TextBox2) Then TextBox2 = "": Exit Sub
If CDate(TextBox2) < CDate("1/" & TextBox1) Or _
    CDate(TextBox2) > DateSerial(Year("1/" & TextBox1), Month("1/" & TextBox1) + 1, 0) Then TextBox2 = ""
End Sub
La macro si les 2 TextBoxes sont dans un UserForm :
VB:
Private Sub TextBox2_AfterUpdate()
If Not IsDate("1/" & TextBox1) Then TextBox1 = "": TextBox2 = "": Exit Sub
If Not IsDate(TextBox2) Then TextBox2 = "": Exit Sub
If CDate(TextBox2) < CDate("1/" & TextBox1) Or _
    CDate(TextBox2) > DateSerial(Year("1/" & TextBox1), Month("1/" & TextBox1) + 1, 0) Then TextBox2 = ""
End Sub
A+
 

KTM

XLDnaute Impliqué
Bonjour KTM, mapomme,

Rien n'empêche de mettre l'année avec le mois, exemple "Août 2019" ou "8/19" dans TextBox1.

La macro si les 2 TextBoxes sont dans une feuille de calcul :
VB:
Private Sub TextBox2_LostFocus()
If Not IsDate("1/" & TextBox1) Then TextBox1 = "": TextBox2 = "": Exit Sub
If Not IsDate(TextBox2) Then TextBox2 = "": Exit Sub
If CDate(TextBox2) < CDate("1/" & TextBox1) Or _
    CDate(TextBox2) > DateSerial(Year("1/" & TextBox1), Month("1/" & TextBox1) + 1, 0) Then TextBox2 = ""
End Sub
La macro si les 2 TextBoxes sont dans un UserForm :
VB:
Private Sub TextBox2_AfterUpdate()
If Not IsDate("1/" & TextBox1) Then TextBox1 = "": TextBox2 = "": Exit Sub
If Not IsDate(TextBox2) Then TextBox2 = "": Exit Sub
If CDate(TextBox2) < CDate("1/" & TextBox1) Or _
    CDate(TextBox2) > DateSerial(Year("1/" & TextBox1), Month("1/" & TextBox1) + 1, 0) Then TextBox2 = ""
End Sub
A+
Merci Job75
 

patricktoulon

XLDnaute Barbatruc
bonsoir
juste pour le fun
tu peux intervenir pendant la saisie
dans le textbox1 seulement le chiffre du mois
la chaîne du mois est annulée si ca ne correspond pas au chiffre dans le textbox1 et il devient rouge
la frappe est annulée aussi si tu oublie les "/"
l'année n'a pas d'importance elle est substituée et j'ai pris 2000 au cas ou tu tape un "29/02"
c'est pas bloquant quand c'est rouge tu retape c'est tout
VB:
Private Sub TextBox2_Change()
    Dim goodDate, compar$
    goodDate = "01/" & Format(TextBox1, "00") & "/2000"
    With TextBox2
        compar = .Value & Mid(goodDate, Len(.Value) + 1)
        If Not IsDate(compar) Then
            .Value = Mid(.Value, 1, Len(.Value) - IIf(Len(.Value) >= 1, 1, 0)): Beep:
        Else
            If Month(CDate(compar)) <> Month(CDate(goodDate)) Then .Value = Mid(.Value, 1, 3): Beep: .BackColor = vbRed Else .BackColor = vbWhite
        End If
    End With
End Sub

pour le visuel tu peux faire un. sellength aussi ca sélectionne le mois en erreur
ligne a remplacer
Code:
If Month(CDate(compar)) <> Month(CDate(goodDate)) Then .SelStart = 3: .SelLength = 10: Beep: .BackColor = vbRed Else .BackColor = vbWhite
le principe est assez simple en fait
compar=le textbox2 + la partie manquante de gooddate en terme de chaîne
si c'est pas une date -->-1 caractère
si le mois n'est pas bon on garde que les 3 premiers caractères
teste dans un userform
 
Dernière édition:

KTM

XLDnaute Impliqué
bonsoir
juste pour le fun
tu peux intervenir pendant la saisie
dans le textbox1 seulement le chiffre du mois
la chaîne du mois est annulée si ca ne correspond pas au chiffre dans le textbox1 et il devient rouge
la frappe est annulée aussi si tu oublie les "/"
l'année n'a pas d'importance elle est substituée et j'ai pris 2000 au cas ou tu tape un "29/02"
c'est pas bloquant quand c'est rouge tu retape c'est tout
VB:
Private Sub TextBox2_Change()
    Dim goodDate, compar$
    goodDate = "01/" & Format(TextBox1, "00") & "/2000"
    With TextBox2
        compar = .Value & Mid(goodDate, Len(.Value) + 1)
        If Not IsDate(compar) Then
            .Value = Mid(.Value, 1, Len(.Value) - IIf(Len(.Value) >= 1, 1, 0)): Beep:
        Else
            If Month(CDate(compar)) <> Month(CDate(goodDate)) Then .Value = Mid(.Value, 1, 3): Beep: .BackColor = vbRed Else .BackColor = vbWhite
        End If
    End With
End Sub
Merci
pour le visuel tu peux faire un. sellength aussi ca sélectionne le mois en erreur
ligne a remplacer
Code:
If Month(CDate(compar)) <> Month(CDate(goodDate)) Then .SelStart = 3: .SelLength = 10: Beep: .BackColor = vbRed Else .BackColor = vbWhite
le principe est assez simple en fait
compar=le textbox2 + la partie manquante de gooddate en terme de chaîne
si c'est pas une date -->-1 caractère
si le mois n'est pas bon on garde que les 3 premiers caractères
teste dans un userform
 

job75

XLDnaute Barbatruc
Une autre solution qui impose le format j/mm/aaaa dans TextBox2 :
VB:
Private Sub TextBox2_Change()
Dim x$, y$, z$
If Not IsDate("1/" & TextBox1) Then TextBox1 = "": TextBox2 = "": Exit Sub
x = Mid(CDate("1/" & TextBox1), 3)
y = Left(TextBox2, 1)
z = Left(TextBox2, 2)
If z Like "#/" Then If IsDate(y & x) Then x = y & x
If z Like "##" Then If IsDate(z & x) Then x = z & x
TextBox2 = x
TextBox2.SelStart = InStr(x, "/") - 1
End Sub
 

KTM

XLDnaute Impliqué
Une autre solution qui impose le format j/mm/aaaa dans TextBox2 :
VB:
Private Sub TextBox2_Change()
Dim x$, y$, z$
If Not IsDate("1/" & TextBox1) Then TextBox1 = "": TextBox2 = "": Exit Sub
x = Mid(CDate("1/" & TextBox1), 3)
y = Left(TextBox2, 1)
z = Left(TextBox2, 2)
If z Like "#/" Then If IsDate(y & x) Then x = y & x
If z Like "##" Then If IsDate(z & x) Then x = z & x
TextBox2 = x
TextBox2.SelStart = InStr(x, "/") - 1
End Sub
MERCI CHERS TOUS
Toutes vos Propositions sont excellentes !!!! Encore Merci.
 

Discussions similaires

Réponses
19
Affichages
626

Statistiques des forums

Discussions
312 211
Messages
2 086 299
Membres
103 172
dernier inscrit
Aurelyan