Rien n'empêche de mettre l'année avec le mois, exemple "Août 2019" ou "8/19" dans TextBox1.Le mois ne suffit pas, il faut aussi l'année...
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
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
Merci Job75Bonjour 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 :
La macro si les 2 TextBoxes sont dans un UserForm :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
A+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
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
If Month(CDate(compar)) <> Month(CDate(goodDate)) Then .SelStart = 3: .SelLength = 10: Beep: .BackColor = vbRed Else .BackColor = vbWhite
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
MerciVB: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
le principe est assez simple en faitCode:If Month(CDate(compar)) <> Month(CDate(goodDate)) Then .SelStart = 3: .SelLength = 10: Beep: .BackColor = vbRed Else .BackColor = vbWhite
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
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 TOUSUne 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