+ Reply to Thread
Results 1 to 6 of 6

popup calendar when clicking on cell in userform

Hybrid View

  1. #1
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    popup calendar when clicking on cell in userform

    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!

  2. #2
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: popup calendar when clicking on cell in userform

    Hello Spritz,

    I understand the code you have posted here. But, there are a few questions I have about your setup that will be answered more quickly and easily by seeing the workbook. Can you post the workbook?
    Sincerely,
    Leith Ross

    Remember To Do the Following....

    1. Use code tags. Place [CODE] before the first line of code and [/CODE] after the last line of code.
    2. Thank those who have helped you by clicking the Star below the post.
    3. Please mark your post [SOLVED] if it has been answered satisfactorily.


    Old Scottish Proverb...
    Luathaid gu deanamh maille! (Rushing causes delays!)

  3. #3
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Re: popup calendar when clicking on cell in userform

    Attached an example workbook. Not sure how this works in Excel 2007.

    You should be able to see what i am talking about here. If you press the button to open up the form and go to the inspections tab, if someone accidentally clicks on the date and they click the x or cancel it removes the date. Similarily, if you accidentally click it and SELECT a date, it keeps that date stored and if you were to click another cell with the calendar in it would replace the current date in there with the date that was stored.
    Attached Files Attached Files

  4. #4
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: popup calendar when clicking on cell in userform

    Hello Spritz,

    Seems like everything breaks at once. Both my truck and my daughter's broke down. I have been juggling schedules with one car to get her to work and back plus deal with the repair shops. Anyway, the attached workbook has a new Calendar. I wrote this a few years ago because I had the same problem you did. This Calendar form will let you select the object the date is to be displayed in. The object must have a Value property for this to work. The reason is most objects have a Value property. There are a few with Captions, TextFrames, etc. That will be an update for a later time.

    The attached workbook has these modules and changes added.
    Here is a sample of the changes I made to your TextBox code. You can see there is far less code now.
    Private Sub LI1_Enter()
        Dim D1 As Date
            Call frmCalendar.DisplayIn(LI1)
            If LI1.Value = "" Then Exit Sub
            D1 = DateAdd("m", 6, LI1.Value)
            Textbox6.Value = Format(D1, "Medium Date")
    End Sub
    DateBtnEvents - Class Module for the Calendar Date Buttons
    'Written: February 15, 2011
    'Author:  Leith Ross
    '
    'NOTE:  This Class is used with the Form frmCalendar.
    '       The property DateGoesTo assigns the object
    '       used to display the chosen date.
    '
    '       You must declare the variable DisplayDateObject
    '       As Public in a Module within this VB Project.
    '       The variable holds the object used to display the
    '       Calendar Date. The Object must have a Value property.
    
    
    Private pvtDisplayObject    As Object
    Private pvtParent           As Object
    
    
    Public WithEvents EventHandler As MSForms.CommandButton
    
    
    Private Sub EventHandler_Click()
        If EventHandler.Enabled = False Then Exit Sub
        pvtDisplayObject.Value = EventHandler.ControlTipText
    End Sub
    
    Public Property Set SendDateTo(ByRef Display_Object As Object)
        Set pvtDisplayObject = Display_Object
    End Property
    
    Public Property Set Parent(ByRef Parent_Object As Object)
        If pvtParent Is Nothing Then
            Set pvtParent = Parent_Object
        Else
            MsgBox "This property is Read Only.", vbExclamation
        End If
    End Property
    
    Public Property Get SendDateTo() As Object
        Set SendDateTo = pvtDisplayObject
    End Property
    
    Public Property Get Parent() As Object
        Set Parent = pvtParent
    End Property
    Code for the Calendar Form
    'Written: January 11, 2011
    'Updated: February 15, 2011
    'Author:  Leith Ross (www.excelforum.com)
    
    Option Explicit
    
    Dim BtnArray(1 To 42) As Object
    
    
    Sub DisplayIn(Optional ByRef Display_Object As Object)
        
        ' This sub can be called without having to first load the Calendar. It assigns
        ' an object that will be used to display the Calendar date and then displays
        ' the Calendar itself. The default display object is the ActiveCell.
        '
        ' NOTE: The object used to display the date must have a Value Property.
        
        
        Dim i As Long
        
            Set DisplayDateObject = Display_Object
            
                For i = 1 To 42
                    Set BtnArray(i).SendDateTo = DisplayDateObject
                Next i
            
            Me.Show
            
    End Sub
    
    Sub ShowDays()
    
        Dim FirstDay    As Long
        Dim i           As Long
        Dim LastDay     As Long
        Dim M           As Integer
        Dim N           As Integer
        Dim y           As Integer
      
            y = CLng(cboYear.Value)
            M = cboMonth.ListIndex + 1
            
            FirstDay = DateSerial(y, M, 1)
            LastDay = DateSerial(y, M + 1, 1) - 1&
        
            For i = FirstDay - (Weekday(FirstDay) - 1) To LastDay + (42 - (LastDay - FirstDay) - Weekday(FirstDay))
                N = N + 1
                With Controls("D" & N)
                    .Caption = Format(i, "d")
                    .ControlTipText = Format(i, "Medium Date")
                    .Tag = .ControlTipText
                    If i < FirstDay Or i > LastDay Then
                        .BackColor = vbButtonFace
                        .Enabled = False
                    Else
                        .BackColor = vbInfoBackground
                        .Enabled = True
                        If Weekday(i) = 1 Or Weekday(i) = 7 Then
                            .ForeColor = vbRed
                        Else
                            .ForeColor = vbButtonText
                        End If
                    End If
                    If i = Fix(Now()) Then .BackColor = vbGreen
                End With
            Next i
         
    End Sub
    
    
    Private Sub cboMonth_Change()
        ShowDays
    End Sub
    
    Private Sub cboYear_Click()
        ShowDays
    End Sub
    
    Private Sub UserForm_Initialize()
    
        Dim i           As Long
        Dim YearEnd     As Long
        Dim YearStart   As Long
        
            ' BEFORE RUNNING THIS FORM...
            ' 1. Add the Class module DateBtnEvents
            ' 2. Declare the variable DisplayDateObject
            '    As Public in a Module within this VB Project.
            '    The variable holds the object used to display the
            '    Calendar Date. The Object must have a Value property.
        
            YearStart = 1900
            YearEnd = 2100
    
                For i = 2 To 372 Step 31
                    cboMonth.AddItem Format(i, "mmmm")
                Next i
    
                For i = YearStart To YearEnd
                    cboYear.AddItem CStr(i)
                Next i
        
                cboYear.Value = Format(Now(), "yyyy")
                cboMonth.Value = Format(Now(), "mmmm")
                                                                                                                                                                                   
            If DisplayDateObject Is Nothing Then
                Set DisplayDateObject = ActiveCell
            End If
            
            For i = 1 To 42
                Set BtnArray(i) = New DateBtnEvents
                Set BtnArray(i).Parent = Me.Controls("D" & i)
                Set BtnArray(i).EventHandler = Me.Controls("D" & i)
                Set BtnArray(i).SendDateTo = DisplayDateObject
            Next i
        
    End Sub
    Attached Files Attached Files

  5. #5
    Registered User
    Join Date
    02-21-2013
    Location
    Ontario, Canada
    MS-Off Ver
    Office 2010
    Posts
    96

    Re: popup calendar when clicking on cell in userform

    That's unfortunate to hear about your bad luck
    At least you are pro @ vb :D This is awesome. Thanks Leith!

  6. #6
    Forum Moderator Leith Ross's Avatar
    Join Date
    01-15-2005
    Location
    San Francisco, Ca
    MS-Off Ver
    2000, 2003, & 2010
    Posts
    23,259

    Re: popup calendar when clicking on cell in userform

    Hello Spritz,

    My luck with autos is bad. Thanks for the condolences. Glad to hear you like the changes.

+ Reply to Thread

Thread Information

Users Browsing this Thread

There are currently 1 users browsing this thread. (0 members and 1 guests)

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts

Search Engine Friendly URLs by vBSEO 3.6.0 RC 1