【问题标题】:Setting combobox values on initialisation and change of other comboboxes在初始化和更改其他组合框时设置组合框值
【发布时间】:2016-09-24 05:58:29
【问题描述】:

(编辑:澄清一下,我运行的是 Excel 2013,所以微软的日期选择器不可用。)

我正在尝试编写一个简单的日期选择器 - 完成后它会更整洁,在我构建它时它只是为了简单起见 - 并且所有内容都按应有的方式填充:

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

但是,有两种情况我想为年、月和日组合框设置默认值。在我使用旋转按钮的时候,一个简单的.Value 语句将它们设置为_Initialize 中的中午12 点。但是.Value.Text 都不适用于组合框:

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

类似地,当我在选择天数较少的月份时尝试将日期设置为较低的值(例如,为了避免返回 2 月 31 日),.Value.Text 再次证明无济于事:

有什么方法可以可靠地设置默认值,然后在代码中更改组合框的值?我是否遗漏了一些非常明显的东西?

编辑:作为参考,表单相关部分的完整代码(UpdatePreview 只是更新了确定按钮上方的预览日期):

--------------------------------------------------------------------------------

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

事情没有奏效:

  • 添加 Microsoft 自己的日期选择器(在 Excel 2013 中不可用)
  • 按照 Microsoft 的建议安装补充日期选择器(不能假设它会在用户的计算机上可用)
  • 尝试通过 VBA 直接设置组合框的 .Text.Value 属性,无论使用何种数据类型。
  • 尝试直接使用日期 (=Month(Now()))、通过变量 (=sNowMonth) 或通过列表索引 (Me.Combo_Month.Text=Me.Combo_Month.List(Month(Now())-1))。

自上周以来,我一直在寻找解决方案。我发现的所有可能性都适用于旧版本的 Office。有人可以帮忙吗?

【问题讨论】:

  • 还有其他想法吗?

标签: excel combobox excel-2013 userform vba


【解决方案1】:

要直接回答您的问题,您可以创建一个日期变量,然后将其转换为文本字符串:

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  

但根据您打算使用它的目的,将输入格式更改为 .Date 可能更聪明

转到工具,附加控件,选择 Microsoft Monthview Control 6.0 (SP6) 并在表单中插入一个日期选择器。

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

【讨论】:

  • 哦,当然 - Year() 正在输出一个日期,所以 2012 年的日期将与 value 2012 年不匹配。
  • 遗憾的是,Excel 2013 中不存在 Monthview 控件及其同类控件。这是最讨厌的。我什至不能使用附加日期选择器,因为此工作簿旨在分发给无法安装它的人。 :-(
  • 恐怕还是不行。我尝试使用变量StringIntegerDate,并尝试将它们(以各种形式)写入.Text.Value,但我不断收到"无法设置 (Text/Value) 属性。无效的属性值。” 我很困惑。 :-(
  • 您是否在组合框上设置了控制源?如果
  • .... 但是,如果您有一个控制源并且 matchRequired 为 True,那么您最好匹配工作表中的日期并将该单元格链接为默认值 - 那么至少您确定您的格式正确,并且与列表中的值完全匹配.... PS:您的下拉菜单是否正常工作?
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-05-20
  • 2013-11-18
  • 1970-01-01
  • 1970-01-01
  • 2010-11-19
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多