Hi,
I found this popup calendar on here a while ago (maybe a month?) that worked great for an application i needed it for. I am now having trouble with it and was hoping someone could shed some light on my problems 
What i have is a userform with 5 textboxes to enter dates in. The calendar pops up for each one however if someone accidentally clicks inside the textbox and wants to his the cancel button or exit it, it clears the date that is currently inside that textbox. This is obviously an issue. I noticed that the value the calendar is pulling is just stored using a tag i am assuming. So if i pick June 14th on the first textbox and then click on the next textbox and just hit cancel on that calendar it will still insert June 14th into the second textbox instead of leaving it alone. Here is all the code relevant to the calendar:
Class Module:
Option Explicit
'use a class module to create a collection of commandbuttons
Public WithEvents CmdBtnGroup As MSForms.CommandButton
Sub CmdBtnGroup_Click()
If Month(CDate(CmdBtnGroup.Tag)) <> frmCalendar.CB_Mth.ListIndex + 1 Then
Select Case _
MsgBox("The selected date is not in the currently selected month." _
& vbNewLine & "Continue?", _
vbYesNo Or vbExclamation Or vbDefaultButton1, "Date check")
Case vbYes
If g_bForm Then
GoTo on_Form
Else: GoTo addDate
End If
Case vbNo
Exit Sub
End Select
Else:
If g_bForm Then
GoTo on_Form
Else: GoTo addDate
End If
addDate:
With ActiveCell
.Value = CDate(CmdBtnGroup.Tag)
.EntireColumn.AutoFit
End With
GoTo chg_month
on_Form:
g_sDate = CmdBtnGroup.Tag <- This is what is storing the date i think
chg_month:
With frmCalendar.CB_Mth
.ListIndex = Month(CmdBtnGroup.Tag) - 1
End With
End If
Unload frmCalendar
End Sub
Global variables:
Option Explicit
Global g_sDate As String
Global g_bForm As Boolean
Calendar Form code:
Option Explicit
Dim Buttons() As New clsCmdButton
Sub Show_Cal()
'use class module to create commandbutton collection, then show calendar
Dim iCmdBtns As Integer
Dim ctl As Control
iCmdBtns = 0
For Each ctl In frmCalendar.Controls
If TypeName(ctl) = "CommandButton" Then
If ctl.Name <> "CB_Close" Then
iCmdBtns = iCmdBtns + 1
ReDim Preserve Buttons(1 To iCmdBtns)
Set Buttons(iCmdBtns).CmdBtnGroup = ctl
End If
End If
Next ctl
frmCalendar.Show
End Sub
Private Sub CB_Close_Click()
Unload frmCalendar
End Sub
Sub addDate()
ActiveCell.Value = Parent
End Sub
Private Sub UserForm_Initialize()
Dim i As Long
Dim lYearsAdd As Long
Dim lYearStart As Long
lYearStart = Year(Date) - 10
lYearsAdd = Year(Date) + 10
With Me
For i = 1 To 12
.CB_Mth.AddItem Format(DateSerial(Year(Date), i, 1), "mmmm")
Next
For i = lYearStart To lYearsAdd
.CB_Yr.AddItem Format(DateSerial(i, 1, 1), "yyyy")
Next
.Tag = "Calendar"
.CB_Mth.ListIndex = Month(Date) - 1
.CB_Yr.ListIndex = Year(Date) - lYearStart
.Tag = ""
End With
Call Build_Calendar
End Sub
Private Sub CB_Mth_Change()
If Not Me.Tag = "Calendar" Then Build_Calendar
End Sub
Private Sub CB_Yr_Change()
If Not Me.Tag = "Calendar" Then Build_Calendar
End Sub
Sub Build_Calendar()
Dim i As Integer
Dim dTemp As Date
Dim dTemp2 As Date
Dim iFirstDay As Integer
With Me
.Caption = " " & .CB_Mth.Value & " " & .CB_Yr.Value
dTemp = CDate("01/" & .CB_Mth.Value & "/" & .CB_Yr.Value)
iFirstDay = WeekDay(dTemp, vbSunday)
.Controls("D" & iFirstDay).SetFocus
For i = 1 To 42
With .Controls("D" & i)
dTemp2 = DateAdd("d", (i - iFirstDay), dTemp)
.Caption = Format(dTemp2, "d")
.Tag = dTemp2
.ControlTipText = Format(dTemp2, "dd/mm/yy")
'add dates to the buttons
If Format(dTemp2, "mmmm") = CB_Mth.Value Then
If .BackColor <> &H80000016 Then .BackColor = &H80000018
If Format(dTemp2, "m/d/yy") = Format(Date, "m/d/yy") Then .SetFocus
.Font.Bold = True
Else
If .BackColor <> &H80000016 Then .BackColor = &H8000000F
.Font.Bold = False
End If
'format the buttons
End With
Next
End With
End Sub
Code inside each textbox to open calendar:
Private Sub LI1_Enter()
g_bForm = True
frmCalendar.Show_Cal
LI1.Value = Format(g_sDate, "Medium Date")
end sub
Basically all i want is when i Hit the cancel button on the calendar form, i want it to do NOTHING. Any help is appreciated!
Bookmarks