By: Shamima Sultana

In reply to Asger.

Hello Asger,

It seems like there are still a few challenges with the date format and the auto-hide functionality. Here’s a refined approach:

Let’s force the date format explicitly in each control to avoid any discrepancy. By handling it this way, we ensure all dates display in dd/mm/yyyy.

Update the Create_Calendar procedure as follows:

Sub Create_Calendar()

Dim formatString As String
formatString = "dd/mm/yyyy" ' Enforce a consistent date format

For i = 1 To 42
Dim currentDate As Date
currentDate = DateAdd("d", i - Weekday(CDate(Month_Box.Value & "/1/" & Year_Box.Value)), _
CDate(Month_Box.Value & "/1/" & Year_Box.Value))

Controls("C" & i).Caption = Format(currentDate, "d")
Controls("C" & i).ControlTipText = Format(currentDate, formatString)

' Set highlight for current month dates and today's date
If Format(currentDate, "mmmm") = Month_Box.Value Then
Controls("C" & i).BackColor = &HFFFFFF
Controls("C" & i).Font.Bold = True
If Format(currentDate, formatString) = Format(Date, formatString) Then Controls("C" & i).SetFocus
Else
Controls("C" & i).BackColor = &H8000000F
Controls("C" & i).Font.Bold = False
End If
Next i

End Sub

If the calendar is not hiding after selection, it may be that the event isn’t firing as expected. Try placing the following line inside each button’s event handler where a date is selected:

Private Sub C1_Click()
DateTextBox.Value = Format(DateAdd("d", 0, Calendar1.Value), "dd/mm/yyyy") ' Use consistent date format
Calendar1.Visible = False ' Hide calendar after selecting a date
End Sub

If you encounter issues with function names, ensure they match throughout the code, especially when calling or referencing functions. This should clear up any lingering format or visibility issues.

This should address both format consistency and auto-closing the calendar. Let me know if this resolves it, or if I can assist further!

Regards
ExcelDemy