vlookuping时文本框将日期显示为数字

时间:2019-03-13 03:45:42

标签: excel vba

我创建了一个带有包含多个列和文本框的列表框的用户窗体。通过Application.WorksheetFunction.Vlookup选择列表框中的项目时,文本框的值会更改。但是我不是在日期而是在文本框中输入数字。有人可以帮我解决这个问题吗?

Private Sub ListBox_Change()

    Dim lnItem As Long
    Dim ws, ws2 As Worksheet
    Dim rn2 As Range

    Set ws = Workbooks("TOOLS").Worksheets("TOOLS")
    Set ws2 = Workbooks("TOOLS").Worksheets("CONSOLIDATE")
    Set rn2 = ws2.Range("JOURNAL1")

    Me.TextPurchaseDate.Value = Application.WorksheetFunction.Vlookup(Me.ListBox.List(lnItem, 0), rn2, 3, 0)

 End Sub

1 个答案:

答案 0 :(得分:1)

日期在Excel中以数字形式存储-您在单元格中看到的是格式的版本-如果需要,您可以使用Format()获取文本版本

Private Sub ListBox_Change()

    Dim lnItem As Long
    Dim ws, ws2 As Worksheet
    Dim rn2 As Range, res

    Set ws = Workbooks("TOOLS").Worksheets("TOOLS")
    Set ws2 = Workbooks("TOOLS").Worksheets("CONSOLIDATE")
    Set rn2 = ws2.Range("JOURNAL1")

    res = Application.Vlookup(Me.ListBox.List(lnItem, 0), rn2, 3, 0)

    Me.TextPurchaseDate.Value = Format(res, "mm/dd/yyyy") 'for example

End Sub