在初始化和更改其他combobox上设置combobox值

(编辑:为了澄清,我正在运行Excel 2013,所以微软的dateselect器不可用。)

我正在尝试编写一个简单的dateselect器 – 完成后它会更加整洁,在创build它时它只是一个简单的大事件,而且一切都应该如此:

日期选择器窗体

Me.Combo_Year.List = wsLU.Range("Date_Years").Value Me.Combo_Month.List = wsLU.Range("Date_Months").Value Me.Combo_Day.List = wsLU.Range("Date_Days31").Value 

但是,有两种情况我想为年,月和日combobox设置默认值。 对于我使用旋转button的时候,简单的.Value语句将它们设置为_Initialize 12中午。 但是,也没有.Text为combobox工作:

 Me.Combo_Year.Text = Year(Now()) ' Doesn't work Me.Combo_Month.Text = Month(Now()) ' Doesn't work Me.Combo_Day.Text = Day(Now()) ' Doesn't work Me.Spin_Hour.Value = 12 ' Works fine Me.Spin_Minute.Value = 0 ' Works fine 

初始化时出错

同样,当我selectdate较less的月份时(例如为了避免返回二月三十一日)而将date设置为较低的值时, .Value.Text都不再.Text

更改月份后调试行

有没有办法可靠地设置一个默认值,然后更改代码中的combobox的值? 我错过了一些非常明显的东西吗?

编辑:作为参考,表单的相关部分的完整代码( UpdatePreview只是更新OKbutton上方的预览date)按要求:

 -------------------------------------------------------------------------------- Option Explicit -------------------------------------------------------------------------------- Private Sub UserForm_Initialize() Dim wsLU As Worksheet, wbV As Workbook Set wbV = ActiveWorkbook Set wsLU = wbV.Worksheets("General Lookups") Me.Combo_Year.List = wsLU.Range("Date_Years").Value Me.Combo_Month.List = wsLU.Range("Date_Months").Value Me.Combo_Day.List = wsLU.Range("Date_Days31").Value Me.Combo_Minute.AddItem 0 Me.Combo_Minute.AddItem 30 ' Tried putting the date numbers via variables instead of direct, with various data types Dim TestYear As String, TestMonth As String, TestDay As String TestYear = Year(Now()) TestMonth = Month(Now()) TestDay = Day(Now()) Lab_T_Year.Caption = TestYear Lab_T_Month.Caption = TestMonth Lab_T_Day.Caption = TestDay 'Me.Combo_Year.Text = TestYear ' If these lines are commented out the form will load, though without the comboboxes prepopulated 'Me.Combo_Month.Text = TestMonth ' If these lines are commented out the form will load, though without the comboboxes prepopulated 'Me.Combo_Day.Text = TestDay ' If these lines are commented out the form will load, though without the comboboxes prepopulated ' Original code; tried this both with and without various Format types. 'Me.Combo_Year.Value = Format(Year(Now()), "0000") 'Me.Combo_Month.Value = Format(Month(Now()), "00") 'Me.Combo_Day.Value = Format(Day(Now()), "00") Me.Spin_Hour.Value = 12 Me.Combo_Minute.Value = 0 ' Switched the minute spinner to a combobox as the client wanted to just pick half hours (00/30) instead of minutes UpdatePreview ' Updates date and time preview, works fine. End Sub -------------------------------------------------------------------------------- Private Sub Combo_Year_Change() ' Combo_Month_Change has an equivalent sub that essentially mirrors this one Dim wsLU As Worksheet, wbV As Workbook Set wbV = ActiveWorkbook Set wsLU = wbV.Worksheets("General Lookups") Dim iMonthNo As Integer, iYearNo As Long, iMaxDate As Integer ' Set number of days based on month iMonthNo = Me.Combo_Month.ListIndex + 1 iYearNo = Me.Combo_Year.Value If iMonthNo = 1 Or iMonthNo = 3 Or iMonthNo = 5 Or iMonthNo = 7 Or iMonthNo = 8 Or iMonthNo = 10 Or iMonthNo = 12 Then Me.Combo_Day.List = wsLU.Range("Date_Days31").Value iMaxDate = 31 ElseIf iMonthNo = 4 Or iMonthNo = 6 Or iMonthNo = 9 Or iMonthNo = 11 Then Me.Combo_Day.List = wsLU.Range("Date_Days30").Value iMaxDate = 30 ElseIf iMonthNo = 2 Then Me.Combo_Day.List = wsLU.Range("Date_Days28").Value iMaxDate = 28 ' Leap year div by 4 If iYearNo / 4 = Int(iYearNo / 4) And Not (iYearNo / 100 = Int(iYearNo / 100)) Then Me.Combo_Day.List = wsLU.Range("Date_Days29").Value If iYearNo / 4 = Int(iYearNo / 4) And Not (iYearNo / 100 = Int(iYearNo / 100)) Then iMaxDate = 29 ' Leap year div by 400 If iYearNo / 4 = Int(iYearNo / 4) And iYearNo / 400 = Int(iYearNo / 400) Then Me.Combo_Day.List = wsLU.Range("Date_Days29").Value If iYearNo / 4 = Int(iYearNo / 4) And iYearNo / 400 = Int(iYearNo / 400) Then iMaxDate = 29 End If ' Code to attempt to change the date down if Month is switched to one with fewer days. It doesn't work. If Me.Combo_Day.Value > iMaxDate And iMonthNo > 0 And Not Me.Combo_Day.Value = "" Then Me.Combo_Day.Value = iMaxDate UpdatePreview ' Updates date and time preview, works fine. End Sub 

事情WOT没有工作:

  • 添加Microsoft自己的dateselect器(在Excel 2013中不可用)
  • 按照Microsoft的build议安装补充dateselect器(不能认为它可以在用户的​​计算机上使用)
  • 尝试通过VBA直接设置combobox的.Text.Value属性,而不pipe使用何种数据types。
  • 直接尝试date( =Month(Now()) ),通过variables( =sNowMonth )或列表索引( Me.Combo_Month.Text=Me.Combo_Month.List(Month(Now())-1) )。

自上周以来,我一直在寻找解决办法。 我发现的每种可能性都是针对旧版Office的。 谁能帮忙?

要直接回答你的问题,那么你可以做一个datevariables,然后将其转换为文本string:

 Dim txtNowYear As String Dim txtNowMonth As String Dim txtNowDay As String txtNowYear = Year(Now()) txtNowMonth = Month(Now()) txtNowDay = Day(Now()) Me.Combo_Year.Text = txtNowYear Me.Combo_Month.Text = txtNowMonth Me.Combo_Day.Text = txtNowDay 

但取决于你打算使用它,只需将input格式改为.Date就可以了

转到工具,附加控制,selectMicrosoft Monthview Control 6.0(SP6)并在窗体中插入dateselect器。

PS:类似的方法应该能够处理你的第二个问题。