Option Explicit
Public WithEvents BtJour As msforms.CommandButton
Dim cls(1 To 38) As New Calendar1
Public CellRef As Range
Private Sub Resultat_Change(): Me.Hide: End Sub
Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
If CloseMode = 0 Then Cancel = True: Me.Hide
End Sub
Private Sub UserForm_Deactivate()
If Not IsDate(Me.Resultat.Value) Then Me.Resultat.Value = CellRef.Value
End Sub
Private Sub UserForm_Initialize()
Dim i&, dat As Date
Set CellRef = ActiveCell ' ici tu met ton [A1] du xlam si tu veux!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!!
For i = Year(Date) - 50 To Year(Date) + 10: Me.ComboBox1.AddItem i: Next ' les années pas en dur!!!!
For i = 1 To 12: Me.ComboBox2.AddItem Format(DateSerial(2020, i, 1), "mmmm"): Next
'gestion du non date en celref!!!!!!
If IsDate(CellRef.Value) Then dat = CDate(CellRef.Value) Else dat = Date
Me.ComboBox1.Value = Year(dat)
Me.ComboBox2.ListIndex = Month(dat) - 1
End Sub
Private Sub UserForm_Activate()
Dim i&
For i = 1 To 38: Set cls(i).BtJour = Me.Controls("CommandButton" & i): Next 'classification des 38 boutons pour evenement unique
End Sub
Private Sub BtJour_Click(): Reponse (BtJour.Caption): End Sub ' evenement unique pour les 38 boutons
Private Sub ComboBox1_Change(): repaintDays: End Sub
Private Sub ComboBox2_Change(): repaintDays: End Sub
Private Sub repaintDays() 'des que tu change l'une ou l'autre ca te réinitialise le calendrier
Dim i&, j&, q&, X&, z&
If ComboBox2.Value = "" Or ComboBox1.Value = "" Then Exit Sub
X = Weekday(DateSerial(Me.ComboBox1.Value, Me.ComboBox2.ListIndex + 1, 1), vbUseSystemDayOfWeek)
z = Day(DateSerial(Me.ComboBox1.Value, Me.ComboBox2.ListIndex + 2, 0))
For i = 1 To 38
q = (i >= X) And (j < z) 'q est true si i>=(x) au jour de semaine du 1er du mois et si j< que le dernier jour du mois
With Me.Controls("CommandButton" & i)
.Visible = q 'utilisation de q pour true/false
If q Then j = j + 1: .Caption = j
End With
Next
End Sub
Sub Reponse(monjour)
'On Error Resume Next'inutile il ne peut pas y avoir d'erreur puisque les combos sont jamais vides
Calendar1.Resultat.Value = CDate(DateSerial(Calendar1.ComboBox1.Value, Calendar1.ComboBox2.ListIndex + 1, monjour))
End Sub