【问题标题】:Formatting dates in a combobox dropdown list在组合框下拉列表中格式化日期
【发布时间】:2014-02-21 23:36:34
【问题描述】:

我创建了一个简单的用户表单,其中的组合框填充了一系列日期 (rngWeekList),但我非常头疼,试图让下拉框中的列表以“dd-mmm-yy”格式显示。这是我的代码:

Private Sub UserForm_Initialize()

    ' Populate the list with the date range
    ComboBox1.List = Worksheets("Cover").Range("rngWeekList").Value

    ' Set the defulat selection (based off rngWeekIndex)
    ComboBox1.ListIndex = Worksheets("Cover").Range("rngWeekIndex").Value - 1

    ' Format
    ComboBox1 = Format(ComboBox1, "dd-mmm-yy")

End Sub

Private Sub ComboBox1_Change()
    ' Format 
    ComboBox1 = Format(ComboBox1, "dd-mmm-yy")
End Sub

这可以正确格式化组合框中的选定项目(例如“02-Jul-14”),但是当我打开下拉列表时,显示的所有列表条目都以默认的“m/d/yyyy”格式设置。有没有办法更改列表条目的格式?对于习惯于查看月份前一天的用户来说,这会让人感到困惑。

提前感谢您的帮助,非常感谢。

埃德

【问题讨论】:

    标签: vba date combobox


    【解决方案1】:

    我设法通过遍历组合框中的每个项目并对其进行格式化来修复它(如果有更优雅的方法,请随时纠正我!)

    Private Sub UserForm_Initialize()
    Dim i As Integer
    
        ' Populate the list with the date range
        ComboBox1.List = Worksheets("Cover").Range("rngWeekList").Value
    
        'Format all items
        For i = 0 To ComboBox1.ListCount - 1
            ComboBox1.List(i) = Format(DateValue(ComboBox1.List(i)), "dd-mmm-yy")
        Next i
    
        ' Set the default selection (based off rngWeekIndex)
        ComboBox1.ListIndex = Worksheets("Cover").Range("rngWeekIndex").Value - 1
    
    End Sub
    
    Private Sub ComboBox1_Change()
        ' Format the selection
        ComboBox1 = Format(ComboBox1, "dd-mmm-yy")
    End Sub
    

    很抱歉发帖,但我真的以为我卡住了。

    再次感谢,

    埃德

    【讨论】:

      猜你喜欢
      • 2013-03-21
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多