Public myDate As Date
Sub DateSelector()
Range("AB2").NumberFormat = "DDDD DD MMMM YYYY"
Dim datetoday%
datetoday = MsgBox("Is this break list for today, " & Format(VBA.Date, "DDDD DD MMMM YYYY") & "?", 36, "Please confirm:")
If datetoday = 6 Then
Range("AB2").Value = VBA.Date
Columns(28).AutoFit
Exit Sub
End If
Dim dlgDate As DialogSheet, DateDialog$
Dim i%, strYear$, strMonth$, blnLeapYear As Boolean
DateDialog = "DateSelector"
Application.ScreenUpdating = False
On Error Resume Next
Application.DisplayAlerts = False
ActiveWorkbook.DialogSheets(DateDialog).Delete
Application.DisplayAlerts = True
Err.Clear
Set dlgDate = ActiveWorkbook.DialogSheets.Add
With dlgDate
.Name = DateDialog
.Visible = xlSheetHidden
With .DialogFrame
.Height = 196
.Width = 252
.Caption = "Select a date"
End With
.Labels.Add 90, 50, 26, 16
.Labels(1).Caption = "Year"
.Labels.Add 160, 50, 30, 16
.Labels(2).Caption = "Month"
.Labels.Add 251, 50, 20, 16
.Labels(3).Caption = "Day"
.DropDowns.Add 90, 64, 48, 16
With .DropDowns(1)
For i = -10 To 10
.AddItem Format(DateSerial(Year(Date) + i, 1, 1), "YYYY")
Next i
.ListIndex = 11
End With
.DropDowns.Add 160, 64, 70, 16
With .DropDowns(2)
For i = 1 To 12
.AddItem Format(DateSerial(1, i, 1), "MMMM")
Next i
.ListIndex = Format(Date, "M")
End With
.DropDowns.Add 251, 64, 48, 16
With .DropDowns(3)
For i = 1 To 31
.AddItem i
Next i
.ListIndex = Format(Date, "D")
End With
With .Buttons("Button 2")
.BringToFront
.Left = 90
.Top = 180
.Width = 140
.Height = 20
.Caption = "Yes, select this date"
End With
With .Buttons("Button 3")
.BringToFront
.Left = 251
.Top = 180
.Width = 48
.Height = 20
.Caption = "Cancel"
End With
Application.ScreenUpdating = True
If .Show = True Then
With .DropDowns(1)
strYear = .List(.ListIndex)
End With
With .DropDowns(2)
strMonth = .List(.ListIndex)
End With
If .DropDowns(3).ListIndex = 31 Then
Select Case strMonth
Case "April", "June", "September", "November"
MsgBox "There is no such date as " & strMonth & " " & .DropDowns(3).ListIndex & "." & vbCrLf & _
"Please select a valid date.", 48, strMonth & " only has 30 days."
.Show
End Select
End If
If strMonth = "February" Then
blnLeapYear = IIf(Month(DateSerial(Val(strYear), 2, 29)) = 2, True, False)
If blnLeapYear = False Then
If .DropDowns(3).ListIndex > 28 Then
MsgBox "There is no such date as " & strMonth & " " & .DropDowns(3).ListIndex & "." & vbCrLf & _
"Please select a valid date.", 48, strMonth & " only has 28 days in " & strYear
.Show
End If
Else
If .DropDowns(3).ListIndex > 29 Then
MsgBox "There is no such date as " & strMonth & " " & .DropDowns(3).ListIndex & "." & vbCrLf & _
"Please select a valid date.", 48, strMonth & " only has 29 days in " & strYear
.Show
End If
End If
End If
myDate = DateSerial(.DropDowns(1).List(.DropDowns(1).ListIndex), .DropDowns(2).Value, .DropDowns(3).Value)
If Val(Format(myDate, "DD")) = Val(.DropDowns(3).Value) Then
MsgBox _
"You selected the date of " & Format(myDate, "DDDD DD MMMM YYYY") & "." & vbCrLf & _
"Click OK and it will be entered into cell AB2.", 64, "Confirm date selection"
Range("AB2").Value = myDate
Columns(28).AutoFit
Else
MsgBox "That was 2 attempts at selecting an invalid date." & vbCrLf & "Click OK to exit.", 16, "Cannot continue."
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
Exit Sub
End If
Else
MsgBox "No date was selected.", 64, "You clicked Cancel."
End If
Application.DisplayAlerts = False
.Delete
Application.DisplayAlerts = True
End With
End Sub
Private Sub DeleteDateSelector()
With Application
.ScreenUpdating = False
.DisplayAlerts = False
On Error Resume Next
DialogSheets("DateSelector").Delete
Err.Clear
.DisplayAlerts = True
.ScreenUpdating = True
End With
End Sub