Excel VBA日历控件:寻找选中日期传递至Label6的代码位置
Excel VBA日历控件的日期传递与格式问题
我基于Stack Overflow的问题编写了Excel VBA日历控件代码,目前已实现选中日期写入单元格并关闭日历的功能,运行正常。但发现一个现象:首次启动日历控件时,底部日期格式为yyyy/mm/dd;选中日期后,格式变为dd/mm/yyyy。
我现在有两个核心疑问:
- 哪段代码负责将日历中选中的日期传递至Label6?
- 我当前通过
PickedDate = Label6.Caption从Label6的Caption属性获取选中日期,但该方式高度依赖区域日期格式(如dd/mm/yy、yy/mm/dd等),如何能直接传递日期值而非字符串?
在frmCalendar代码中,我找到了以下和ShortDateFormat属性相关的代码,但存在疑惑:
Public Property Let ShortDateFormat(s As String) SdtFormat = s Label6.Caption = Format(Date, SdtFormat) End Function Public Property Get ShortDateFormat() As String ShortDateFormat = SdtFormat End Property
这里的Date是获取当前日期的内置函数,并非存储选中日期的变量,但这是我找到的唯一给Label6.Caption赋值的代码,我预期此处应该使用选中日期对应的变量。
另外,我猜测相关逻辑可能存在于UserForm_Initialize事件代码中,以下是该代码片段:
Private Sub UserForm_Initialize() With CbLanguage .AddItem "EN" .AddItem "GER" .AddItem "SPA" .AddItem "ITL" .AddItem "FRE" .ListIndex = 0 End With '~~> Hide the Title Bar HideTitleBar Me Me.Caltheme = Venom Me.LongDateFormat = "dddd mmmm dd, yyyy" Me.ShortDateFormat = "dd/mm/yyyy" '~~> Create a command button control array so that '~~> when we press escape, we can unload the userform Dim CBCtl As Control i = 0 For Each CBCtl In Me.Controls If TypeOf CBCtl Is MSForms.CommandButton Then i = i + 1 ReDim Preserve CBArray(1 To i) Set CBArray(i).CommandButtonEvents = CBCtl End If Next CBCtl Set CBCtl = Nothing '~~> Set the Time StartTimer curDate = Date thisDay = Day(Date): thisMonth = Month(Date): thisYear = Year(Date) CurYear = Year(Date): CurMonth = Month(Date) '~~> Populate this months calendar PopulateCalendar curDate End Sub
原Stack Overflow问题中提及代码过长未完整发布,若需特定代码段我可补充提供。
内容的提问来源于stack exchange,提问作者Forward Ed
相关产品推荐
相关产品推荐

