Userform Date Format Textbox - PullRequest
       23

Userform Date Format Textbox

0 голосов
/ 05 апреля 2019

Поэтому я использую пользовательскую форму excel UserForm1, где для определенных полей требуется ввод дат в формате YYYY/MM/DD.Поэтому у меня есть импортированный всплывающий календарь в пользовательскую форму с именем CalenderForm, позволяющий пользователю щелкнуть текстовое поле, и календарь должен появиться прямо рядом с полем.

Я получил календарь с этого сайта: https://trevoreyre.com/portfolio/excel-datepicker/

Проблема, с которой я столкнулся, заключается в преобразовании значения даты из CalenderForm в поле текстового поля в YYYY / MM /Формат даты DD.

Ниже приведена фотография пользовательской формы: enter image description here

Код ниже предназначен для текстового поля перед собранием (отредактировано)

Private Sub tbpremeeting_Change()
    Dim dateVariable As Date
    dateVariable = CalendarForm.GetDate
    Me.tbpremeeting.Text = Format(dateVariable, "yyyy/mm/dd")

End Sub

Календарь выглядит так: enter image description here

1 Ответ

1 голос
/ 05 апреля 2019

Вам необходимо прочитать инструкцию в CalendarForm.frm

«1899/12/30» возвращается, когда пользователь отменяет действие без выбора даты.

'   OkayButton (Boolean) - Controls whether or not the Okay button is visible. If the
'       Okay button is enabled, when the user selects a date, it is highlighted, but
'       is not returned until they click Okay. If the Okay button is disabled,
'       clicking a date will automatically return that date and unload the form.


'''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
    ' UserForm_QueryClose
    '
    ' I originally included this sub to override when the user cancelled the
    ' CalendarForm using the X button, in order to avoid receiving an invalid date value
    ' back from the userform (1/0/1900 12:00:00 AM). This sub sets DateOut to currently
    ' selected Date, or to the initial SelectedDate passed to the GetDate function if user
    ' has not changed the selection, or the Okay button is not enabled.
    '
    ' Note that it is still possible for the CalendarForm to return an invalid date value
    ' if no initial SelectedDate is set, the user does not make any selection, and then
    ' cancels the userform.
    '

        ' I ended up removing the sub, because I like being able to detect if the user has
        ' cancelled the userform by testing the date from it. For instance, if user selects
        ' a date, but then changes their mind and cancels the userform, you wouldn't want to
        ' still return that date to your variable. You would want to revert to their previous
        ' selection, or do some error handling, if necessary.
        '
        ' If you want the functionality described above, of returning the selected date or
        ' initial date if the user cancels, you can un-comment this sub.
        '''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''
        'Private Sub UserForm_QueryClose(Cancel As Integer, CloseMode As Integer)
        '    If CloseMode = 0 Then
        '        Cancel = True
        '        DateOut = SelectedDateIn
        '        Me.Hide
        '    End If
        'End Sub
...