Hello all,
I new to coding and I'm trying to create DTPicker, but everytime i run it i get this error. any help is appreciated. I have uploaded the file.
Here is the code:
Option Explicit
Dim ThisDay As Date
Dim ThisYear, ThisMth As Date
Dim CreateCal As Boolean
Dim i As Integer
Private Sub Cb_month_Change()
End Sub
Private Sub UserForm_Initialize()
Application.EnableEvents = False
ThisDay = Date
ThisMth = Format(ThisDay, "mm")
ThisYear = Format(ThisDay, "yyyy")
For i = 1 To 12
Cb_month.AddItem Format(DateSerial(Year(Date), Month(Date) + i, 0), "mmmm")
Next
Cb_month.ListIndex = Format(Date, "mm") - Format(Date, "mm")
For i = -10 To 50
If i = 1 Then Cb_year.AddItem Format((ThisDay), "yyyy") Else Cb_year.AddItem _
Format((DateAdd("yyyy", (i - 1), ThisDay)), "yyyy")
Next
Cb_year.ListIndex = 11
CreateCal = True
Application.EnableEvents = True
Lbl_time.Caption = Format(Time, "h:mm am/pm")
Lbl_date.Caption = Format(Date)
If CreateCal = True Then
UserForm_time.Caption = " " & Cb_month.Value & " " & Cb_year.Value
End If
Call Build_Calendar
End Sub
Private Sub Build_Calendar()
If CreateCal = True Then
For i = 1 To 42
If i < Weekday((Cb_month.Value) & "/1/" & (Cb_year.Value)) Then
Controls("D" & (i)).Caption = Format(DateAdd("D", (i - Weekday((Cb_month.Value) & "/1/" & (Cb_year.Value))), _
((Cb_month.Value) & "/1/" & (Cb_year.Value))), "D")
Controls("D" & (i)).ControlTipText = Format(DateAdd("D", (i - Weekday((Cb_month.Value) & "/1/" & (Cb_year.Value))), _
((Cb_month.Value) & "/1/" & (Cb_year.Value))), "d/m/yy")
ElseIf i >= Weekday((Cb_month.Value) & "/1/" & (Cb_year.Value)) Then
Controls("D" & (i)).Caption = Format(DateAdd("d", (i - Weekday((Cb_month.Value) _
& "/1/" & (Cb_year.Value))), ((Cb_month.Value) & "/1/" & (Cb_year.Value))), "d")
Controls("D" & (i)).ControlTipText = Format(DateAdd("d", (i - Weekday((Cb_month.Value) & "/1/" & (Cb_year.Value))), _
((Cb_month.Value) & "/1/" & (Cb_year.Value))), "m/d/yy")
End If
Next
End If
End Sub
Hi Cesar,
Why bother to build a date picker when you can use existing solutions?
See this one: https://www.myonlinetraininghub.com/excel-calendar-date-picker-for-worksheets-and-userforms