'+------------------------------------------------------------+
'| VbaA2z - DatePickerX S-1.0 | 10/18/2020 |
'| Compatible with 32-Bit and 64-Bit Office |
'| Author: L. Pamai (VbaA2z.Team@gmail.com) |
'| Visit channel: Youtube.com/VbaA2z |
'| More download: VbaA2z.Blogspot.com |
'+------------------------------------------------------------+
'| Free for personal and commercial use at your own risk |
'+------------------------------------------------------------+
Option Explicit
Dim DatePickerX_Ctrls() As cDatePickerX
Private Sub UserForm_Initialize()
DatePickerX_Ini
GetDate
End Sub
Sub PX_hide()
On Error Resume Next
DatePickerX.Visible = False
On Error GoTo 0
End Sub
Function GetDate()
'date picker loader
Dim k As control
With Me.DatePickerX
.Visible = True
End With
End Function
Function DatePickerX_PrevNext(showNxt As Boolean)
Dim tmpDate As Date, vNewMonthDate As Date
tmpDate = DateSerial(Me.mem_year.Value, Me.mem_mth.Value, 1)
If showNxt = True Then
vNewMonthDate = DateAdd("m", 1, tmpDate)
Else
vNewMonthDate = DateAdd("m", -1, tmpDate)
End If
Call LoadDates(Month(vNewMonthDate), Year(vNewMonthDate))
End Function
Private Sub CommandButton1_Click()
Unload Me
End Sub
Private Sub eCalNextMonth_Click()
DatePickerX_PrevNext True
End Sub
Private Sub eCalPrevMonth_Click()
DatePickerX_PrevNext False
End Sub
Private Sub eCalTitle_Click()
tbYear.Visible = True
mthsCB.Visible = True
mthsCB.Height = 133.9
End Sub
Private Sub eCalToday_Click()
Call LoadDates(Month(Date), Year(Date))
End Sub
Private Sub mthsCB_Click()
Me.eCalTitle.Caption = Me.mthsCB.Value & " " & Me.tbYear.Value
tbYear.Visible = False
mthsCB.Visible = False
'Call LoadDates(mthnobytext(mthsCB.Value), tbYear.Value)
Call LoadDates(mthsCB.Column(1), tbYear.Value)
End Sub
Private Sub tbYear_Change()
If Len(tbYear.Value) >= 4 Then
tbYear.Value = Left(tbYear.Value, 4)
End If
End Sub
Private Sub tbYear_KeyPress(ByVal KeyAscii As MSForms.ReturnInteger)
Select Case KeyAscii
Case 48 To 57
If Len(tbYear) >= 4 Then
KeyAscii = 0
End If
Case Else
KeyAscii = 0
End Select
End Sub
Sub DatePickerX_Ini()
Dim Obj As Object, CtrlPointer As Long
'------------------------------------------------------
Me.tbYear.Value = Year(Date)
'With mthsCB
' .Clear
' .AddItem "January"
' .AddItem "February"
' .AddItem "March"
' .AddItem "April"
' .AddItem "May"
' .AddItem "June"
' .AddItem "July"
' .AddItem "August"
' .AddItem "September"
' .AddItem "October"
' .AddItem "November"
' .AddItem "December"
mthsCB.ColumnCount = 1
Dim xMonths(1 To 12, 1 To 2) As String
Dim i As Integer, j As Integer
xMonths(1, 1) = Format(DateSerial(2020, 1, 1), "MMMM")
xMonths(2, 1) = Format(DateSerial(2020, 2, 1), "MMMM")
xMonths(3, 1) = Format(DateSerial(2020, 3, 1), "MMMM")
xMonths(4, 1) = Format(DateSerial(2020, 4, 1), "MMMM")
xMonths(5, 1) = Format(DateSerial(2020, 5, 1), "MMMM")
xMonths(6, 1) = Format(DateSerial(2020, 6, 1), "MMMM")
xMonths(7, 1) = Format(DateSerial(2020, 7, 1), "MMMM")
xMonths(8, 1) = Format(DateSerial(2020, 8, 1), "MMMM")
xMonths(9, 1) = Format(DateSerial(2020, 9, 1), "MMMM")
xMonths(10, 1) = Format(DateSerial(2020, 10, 1), "MMMM")
xMonths(11, 1) = Format(DateSerial(2020, 11, 1), "MMMM")
xMonths(12, 1) = Format(DateSerial(2020, 12, 1), "MMMM")
xMonths(1, 2) = 1
xMonths(2, 2) = 2
xMonths(3, 2) = 3
xMonths(4, 2) = 4
xMonths(5, 2) = 5
xMonths(6, 2) = 6
xMonths(7, 2) = 7
xMonths(8, 2) = 8
xMonths(9, 2) = 9
xMonths(10, 2) = 10
xMonths(11, 2) = 11
xMonths(12, 2) = 12
mthsCB.Clear
mthsCB.List = xMonths
'End With
'------------------------------------------------------
Me.DatePickerX.Visible = False
ActiveUF = Me.Name
Call LoadDates(Month(Date), Year(Date))
DatePickerX.BackColor = DatePickerX_Back
Me.eCalTitle.ForeColor = DatePickerX_Title_Font
'------------------------------------------------------
ReDim DatePickerX_Ctrls(1 To Me.Controls.Count)
For Each Obj In Me.Controls
If TypeName(Obj) = "Label" And (Obj.Tag = "daysbg" Or Obj.Tag = "days") Then
CtrlPointer = CtrlPointer + 1
Set DatePickerX_Ctrls(CtrlPointer) = New cDatePickerX
Set DatePickerX_Ctrls(CtrlPointer).aMenu = Obj
End If
Next Obj
ReDim Preserve DatePickerX_Ctrls(1 To CtrlPointer)
'------------------------------------------------------
End Sub
Function LoadDates(mth As Byte, yearX As Integer)
Dim nDate As Date, dayNo As String, lDate As Date, mthNo As Byte, yrNo As Byte, kDate As Date, i As Long, dayX As Long
'------------------------------------------------------
nDate = DateSerial(yearX, mth, 1)
dayNo = Weekday(nDate, 0) 'daybyNo(Format(nDate, "DDD"))
lDate = dhLastDayInMonth(nDate)
Me.eCalTitle.Caption = Format(nDate, "MMMM YYYY")
Me.mem_mth = Month(nDate)
Me.mem_year = Year(nDate)
'for non english days. update day header here.
Me.Controls("D" & 1).Caption = "S"
Me.Controls("D" & 2).Caption = "M"
Me.Controls("D" & 3).Caption = "T"
Me.Controls("D" & 4).Caption = "W"
Me.Controls("D" & 5).Caption = "T"
Me.Controls("D" & 6).Caption = "F"
Me.Controls("D" & 7).Caption = "S"
'------------------------------------------------------
dayX = 1
'reset
For i = 1 To 42
Me.Controls("day" & i).ForeColor = Color_Dates_Font
dayX = dayX + 1
Next i
dayX = 1
kDate = nDate
For i = dayNo To 42
Me.Controls("day" & i).Caption = Day(kDate) 'CInt(Format(kDate, "DD"))
If kDate <> Date Then
Me.Controls("s" & i).BackColor = Color_Dates_Back
Else
Me.Controls("s" & i).BackColor = Color_CDate_Backcolor
End If
Me.Controls("day" & i).ForeColor = Color_Dates_Font
Me.Controls("s" & i).ControlTipText = kDate
Me.Controls("day" & i).ControlTipText = kDate
If kDate > lDate Then
Me.Controls("day" & i).ForeColor = Color_ODates_Font
End If
dayX = dayX + 1
kDate = kDate + 1
Next i
'------------------------------------------------------
'prior dates
kDate = nDate
If dayNo > 1 Then
For i = dayNo - 1 To 1 Step -1
Me.Controls("day" & i).Caption = Day(kDate - 1) 'CInt(Format(kDate - 1, "DD"))
Me.Controls("s" & i).ControlTipText = kDate - 1
Me.Controls("day" & i).ControlTipText = kDate - 1
Me.Controls("day" & i).ForeColor = Color_ODates_Font
dayX = dayX + 1
kDate = kDate - 1
Next i
End If
Dim m_d1 As Byte
m_d1 = Day(nDate)
'------------------------------------------------------
End Function