Here is the calendar code.
Public targetTextBox As MSForms.TextBox
Private Sub BtnDay_Click()
If Not targetTextBox Is Nothing Then
' Construct the full date based on selected day, current month, and year
Dim selectedDate As Date
selectedDate = DateSerial(CInt(lblYear.Caption), MonthNumber(lblMonth.Caption), CInt(Me.ActiveControl.Caption))
' Assign the formatted date to the target textbox
targetTextBox.Value = Format(selectedDate, "mm/dd/yy")
' Close the calendar
Unload Me
Here is the calendar code.
Public targetTextBox As MSForms.TextBox
Private Sub BtnDay_Click()
If Not targetTextBox Is Nothing Then
' Construct the full date based on selected day, current month, and year
Dim selectedDate As Date
selectedDate = DateSerial(CInt(lblYear.Caption), MonthNumber(lblMonth.Caption), CInt(Me.ActiveControl.Caption))
' Assign the formatted date to the target textbox
targetTextBox.Value = Format(selectedDate, "mm/dd/yy")
' Close the calendar
Unload Me
Else
MsgBox "No target textbox set!", vbExclamation
End If
End Sub
Private Sub UserForm_Initialize()
' Default to the current month and year
lblMonth.Caption = Format(Date, "MMMM") ' Full month name (e.g., "March")
lblYear.Caption = Year(Date) ' Current year
' Initialize the calendar view with days of the current month
UpdateCalendar
End Sub
Private Sub btnPrevMonth_Click()
Dim currentMonth As Integer
Dim currentYear As Integer
Dim newDate As Date
' Get current month and year
currentMonth = Month(DateValue("1 " & lblMonth.Caption & " " & lblYear.Caption))
currentYear = CInt(lblYear.Caption)
' Decrement the month
If currentMonth = 1 Then
currentMonth = 12
currentYear = currentYear - 1
Else
currentMonth = currentMonth - 1
End If
' Update labels
lblMonth.Caption = monthName(currentMonth)
lblYear.Caption = currentYear
' Refresh calendar display
UpdateCalendar
End Sub
Private Sub btnNextMonth_Click()
Dim currentMonth As Integer
Dim currentYear As Integer
Dim newDate As Date
Dim monthText As String
Dim yearText As String
monthText = lblMonth.Caption
yearText = lblYear.Caption
' Validate month and year before converting
If monthText = "" Or yearText = "" Then Exit Sub ' Prevent errors if labels are empty
' Validate that year is numeric
If Not IsNumeric(yearText) Then Exit Sub
' Convert month name to number safely
currentMonth = MonthNumber(monthText)
currentYear = Val(yearText)
If currentMonth = 0 Or currentYear < 1900 Then Exit Sub ' Ensure valid values
currentYear = CInt(lblYear.Caption)
' Increment the month
If currentMonth = 12 Then
currentMonth = 1
currentYear = currentYear + 1
Else
currentMonth = currentMonth + 1
End If
' Update labels
lblMonth.Caption = monthName(currentMonth)
lblYear.Caption = currentYear
' Refresh calendar display
UpdateCalendar
End Sub
Private Sub UpdateCalendar()
Dim firstDay As Integer, totalDays As Integer
Dim i As Integer, startIndex As Integer
Dim currentMonth As Integer, currentYear As Integer
Dim currentDate As Date
' Ensure lblMonth and lblYear contain valid data
If lblMonth.Caption = "" Or lblYear.Caption = "" Then Exit Sub
If Not IsNumeric(lblYear.Caption) Then Exit Sub
' Get current month and year
currentMonth = MonthNumber(lblMonth.Caption)
currentYear = CInt(lblYear.Caption)
' Get first day of the month (1 = Sunday, 7 = Saturday)
currentDate = DateSerial(currentYear, currentMonth, 1)
firstDay = Weekday(currentDate, vbSunday) ' Adjusted for Sunday start
' Get the total number of days in the current month
totalDays = Day(DateSerial(currentYear, currentMonth + 1, 0))
' Determine where the first day of the month should start in btnDay1-42
startIndex = firstDay ' Since btnDay1 starts on a Sunday, no need for adjustments
' Reset all buttons
For i = 1 To 42
With Me.Controls("btnDay" & i)
.Caption = ""
.Visible = False
End With
Next i
' Populate the calendar with actual dates
For i = 1 To totalDays
With Me.Controls("btnDay" & (startIndex + i - 1))
.Caption = i
.Visible = True
End With
Next i
End Sub
Private Function MonthNumber(monthName As String) As Integer
Dim monthList As Variant
monthList = Array("January", "February", "March", "April", "May", "June", _
"July", "August", "September", "October", "November", "December")
Dim i As Integer
For i = LBound(monthList) To UBound(monthList)
If monthList(i) = monthName Then
MonthNumber = i + 1
Exit Function
End If
Next i
MonthNumber = 0 ' Return 0 if no match (prevents errors)
End Function
Calendar pops up but not passing the date to the textboxvusing the latest code that you provided.
Else
MsgBox "No target textbox set!", vbExclamation
End If
End Sub
Private Sub UserForm_Initialize()
' Default to the current month and year
lblMonth.Caption = Format(Date, "MMMM") ' Full month name (e.g., "March")
lblYear.Caption = Year(Date) ' Current year
' Initialize the calendar view with days of the current month
UpdateCalendar
End Sub
Private Sub btnPrevMonth_Click()
Dim currentMonth As Integer
Dim currentYear As Integer
Dim newDate As Date
' Get current month and year
currentMonth = Month(DateValue("1 " & lblMonth.Caption & " " & lblYear.Caption))
currentYear = CInt(lblYear.Caption)
' Decrement the month
If currentMonth = 1 Then
currentMonth = 12
currentYear = currentYear - 1
Else
currentMonth = currentMonth - 1
End If
' Update labels
lblMonth.Caption = monthName(currentMonth)
lblYear.Caption = currentYear
' Refresh calendar display
UpdateCalendar
End Sub
Private Sub btnNextMonth_Click()
Dim currentMonth As Integer
Dim currentYear As Integer
Dim newDate As Date
Dim monthText As String
Dim yearText As String
monthText = lblMonth.Caption
yearText = lblYear.Caption
' Validate month and year before converting
If monthText = "" Or yearText = "" Then Exit Sub ' Prevent errors if labels are empty
' Validate that year is numeric
If Not IsNumeric(yearText) Then Exit Sub
' Convert month name to number safely
currentMonth = MonthNumber(monthText)
currentYear = Val(yearText)
If currentMonth = 0 Or currentYear < 1900 Then Exit Sub ' Ensure valid values
currentYear = CInt(lblYear.Caption)
' Increment the month
If currentMonth = 12 Then
currentMonth = 1
currentYear = currentYear + 1
Else
currentMonth = currentMonth + 1
End If
' Update labels
lblMonth.Caption = monthName(currentMonth)
lblYear.Caption = currentYear
' Refresh calendar display
UpdateCalendar
End Sub
Private Sub UpdateCalendar()
Dim firstDay As Integer, totalDays As Integer
Dim i As Integer, startIndex As Integer
Dim currentMonth As Integer, currentYear As Integer
Dim currentDate As Date
' Ensure lblMonth and lblYear contain valid data
If lblMonth.Caption = "" Or lblYear.Caption = "" Then Exit Sub
If Not IsNumeric(lblYear.Caption) Then Exit Sub
' Get current month and year
currentMonth = MonthNumber(lblMonth.Caption)
currentYear = CInt(lblYear.Caption)
' Get first day of the month (1 = Sunday, 7 = Saturday)
currentDate = DateSerial(currentYear, currentMonth, 1)
firstDay = Weekday(currentDate, vbSunday) ' Adjusted for Sunday start
' Get the total number of days in the current month
totalDays = Day(DateSerial(currentYear, currentMonth + 1, 0))
' Determine where the first day of the month should start in btnDay1-42
startIndex = firstDay ' Since btnDay1 starts on a Sunday, no need for adjustments
' Reset all buttons
For i = 1 To 42
With Me.Controls("btnDay" & i)
.Caption = ""
.Visible = False
End With
Next i
' Populate the calendar with actual dates
For i = 1 To totalDays
With Me.Controls("btnDay" & (startIndex + i - 1))
.Caption = i
.Visible = True
End With
Next i
End Sub
Private Function MonthNumber(monthName As String) As Integer
Dim monthList As Variant
monthList = Array("January", "February", "March", "April", "May", "June", _
"July", "August", "September", "October", "November", "December")
Dim i As Integer
For i = LBound(monthList) To UBound(monthList)
If monthList(i) = monthName Then
MonthNumber = i + 1
Exit Function
End If
Next i
MonthNumber = 0 ' Return 0 if no match (prevents errors)
End Function