Search code examples
vbaexceldatepickermonthcalendar

UserForm adding DatePicker to Monthview and populating ActiveCell with data


I am using a MonthView form in VBA that allows the user to right-click and select InsertDate and choose from the form the date which will then populate into the Active Cell. This code is working fine. The issue is I need to add the time as well (military) which will succeed the date (i.e. 04/23/2014 18:11).

I am using the DateTimePicker form in VBA and have added this to the form. Now I have a monthview and a DT Picker that is custom formatted for HH:mm. The code of the entire user form is listed below. The other part of the code is in "ThisWorkbook" which simply builds the context menu button "Insert Date" and attaches calendar and a Module that shows the calendar. My question is in the Sub DTPicker. I want the values of both the MonthView and the DTPicker to populate the active cell, using the format mentioned above. Any suggestions?

Thanks in Advance!!

`Private Sub CommandButton1_Click()
Unload Me
End Sub


Private Sub DTPicker1_CallbackKeyDown(ByVal KeyCode As Integer, ByVal Shift As Integer, ByVal CallbackField As String, CallbackDate As Date)
On Error Resume Next

??????


End Sub


Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
On Error Resume Next
    ActiveCell.value = DateClicked
    Unload Me
End Sub

Private Sub UserForm_Click()

End Sub


Private Sub UserForm_Initialize1()
    If IsDate(ActiveCell.value) Then
        Me.MonthView1.value = ActiveCell
    Else
        Me.MonthView1.value = Now
    End If
End Sub



Private Sub UserForm_Activate()
    With frmCalendar
        .Top = Application.Top + 340
        .Left = Application.Left + 345
    End With
End Sub`

Solution

  • I am not sure exactly where the date and the time picker will insert the values and how the values they return look like exactly, but you can try the below way to format into waat you need:

    Set the value returned by the DT Picker to a variable, then

    b = Format(a, "m/d/yyyy HH:mm")

    Now write the value of b to the cell as required.

    Hope that helps:)

    EDIT:

    What about this:

    a = Worksheets("Sheet1").Range("A1") ' value from MonthView
    b = Worksheets("Sheet1").Range("B1") ' value from Date Picker
    
    c = Format(a + b, "m/d/yyyy HH:mm") ' combined value in the requested format.
    
    Worksheets("Sheet1").Range("D5").Value = c
    

    Screen http://im68.gulfup.com/b01sVD.png

    EDIT 2:

    I have gone the through the video in the link that you have posted in the comments, and assuming you want to insert the current time along with the date selected you can update the below part of your code:

    Private Sub MonthView1_DateClick(ByVal DateClicked As Date)
    On Error Resume Next   
    a = DateClicked
    b = Time
    c = Format(a + b, "m/d/yyyy HH:mm") ' combined value in the requested format.
    ActiveCell.value = c
    Unload Me
    End Sub
    

    This way when the user selects a date the current time is also added to it in the m/d/yyyy HH:mm format.

    Let me know if this does it for you.