【问题标题】:Userform Date Format Textbox用户表单日期格式文本框
【发布时间】:2019-08-26 17:57:19
【问题描述】:

所以我使用了一个 Excel 用户表单 UserForm1,其中某些字段需要以 YYYY/MM/DD 格式输入日期。因此,我向名为CalenderForm 的用户窗体导入 一个弹出日历,允许用户单击文本框,日历应在字段旁边弹出。

我从这个网站得到日历:https://trevoreyre.com/portfolio/excel-datepicker/

我遇到的问题是将日期值从 CalenderForm 转换为 YYYY/MM/DD 日期格式的文本框字段。

下面是用户表单的图片:

以下代码用于会前文本框(已编辑)

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

End Sub

日历如下所示:

【问题讨论】:

  • Me.tbpremeeting.Text = Format(dateVariable, "yyyy/mm/dd") 您的代码还需要在执行例程之前测试 TBD 或 tbd。之后,如果他们取消,需要更多“待定”
  • @donPablo 编辑了上面的代码,有一个问题,日历没有双击就不会关闭,当点击 X 时,它会将值更改为“1899/12/30”,没有知道为什么不关闭日历

标签: excel vba userform


【解决方案1】:

您需要阅读 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

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2017-06-03
    • 1970-01-01
    • 2016-10-10
    • 2018-07-03
    • 1970-01-01
    相关资源
    最近更新 更多