格式化数组中的日期和时间,还是将数组输出到ListBox之后?

时间:2017-05-22 06:42:34

标签: excel excel-vba excel-2010 vba

我假设无法在数组中格式化时间和日期?如果是,您将如何更改ListBox中列的格式?这是我正在使用的部分。

Private Sub TextBox_Search_Change()
Select Case True
Case OptionButton_User_Name.Value
    temp = UCase(Me.TextBox_Search.Value)
    Dim a()
    Dim rngValues As Variant
    With Sheets("ToolData")
        rngValues = .Range("B2", .Range("B" & .Rows.Count).End(xlUp)).Resize(, 11).Value
    End With
    For i = 1 To UBound(rngValues, 1)
        'Check columns B & F for matching values
        If UCase(rngValues(i, 1)) Like "*" & temp & "*" Then
                        '(i, Colunm being searched)
            'Store columns B, F & G for displaying in the ListBox
            n = n + 1
            ReDim Preserve a(1 To 8, 1 To n)
                        'ListBox (Colunms 1-...)
            a(1, n) = rngValues(i, 1)
            a(2, n) = rngValues(i, 2)
            a(3, n) = rngValues(i, 5)
            a(4, n) = rngValues(i, 6)
            a(5, n) = rngValues(i, 7)
            a(6, n) = rngValues(i, 8)
            a(7, n) = rngValues(i, 9)
            a(8, n) = rngValues(i, 10)
        'ListBox = rngValues(B,+Colunms on sheet)
        End If
    Next
    'If anything found, replace the ListBox contents.  Otherwise leave it as it was.
    If n > 0 Then
        Me.ListBox_History.Column = a
        Me.ListBox_History.Column(3, Me.ListBox_History.ListCount - 1) = Format("hh:mm")
    End If

Case Else

在底部,我将此位添加为测试Me.ListBox_History.Column(3, Me.ListBox_History.ListCount - 1) = Format("hh:mm")但它没有格式化,而是在ListBox的第1行显示hh:mm

1 个答案:

答案 0 :(得分:1)

因此,如果数组中的值不是数字,则应该对其进行格式化。 试试:

a(1, n) = Format(rngValues(i, 1), "hh:mm") 

如果这不起作用,你可以像下面一样通过列:

With ListBox1
For i = 0 To .ListCount - 1
   .List(i, 0) = (Format(.List(i, 0), "hh:mm"))
Next i
End With