将UserForm textbox.value作为日期格式传输到工作表

时间:2018-10-11 18:53:25

标签: excel vba

我做错了。我只想将日期和所有其他数据一起从UserForm保存到列表的末尾。一切正常,但日期格式有问题。

我使用该宏在UserForm文本框中将其设置为“所需”格式:

Private Sub E1GExpiryDate_BeforeUpdate(ByVal Cancel As MSForms.ReturnBoolean)
Dim dDate As Date
  dDate = DateSerial(Year(Date), Month(Date), Day(Date))
  E1GExpiryDate.Value = Format(E1GExpiryDate.Value, "mmm.yyyy")
dDate = E1GExpiryDate.Value
End Sub

要传输的宏:

Private Sub SaveData()
  Dim lRow As Long
  Dim ws As Worksheet
  Set ws = Tabelle1
  lRow = ws.Cells(Rows.Count, 1).End(xlUp).Offset(1, 0).row
With ws
    .Cells(lRow, 1).Value = Me.E1GCharge.Value
    .Cells(lRow, 3).Value = Me.E1GMatName.Value
    .Cells(lRow, 4).Value = Me.E1Gtype.Value
    .Cells(lRow, 5).Value = Me.E1GMatNumber.Value
'copy date.........................
    .Cells(lRow, 6).Value = Format(Me.E1GExpiryDate.Value, "mmm.yyyy")
    .Cells(lRow, 7).Value = Me.E1GBoxPcs.Value
    .Cells(lRow, 8).Value = Me.E1GAmmount.Value
    .Cells(lRow, 9).Value = Me.E1GUnit.Value
    .Cells(lRow, 10).Value = Me.E1Gkonz.Value

End With
'Clear input controls.
  Me.E1GMatName.Value = ""
  Me.E1Gtype.Value = ""
  Me.E1GMatNumber.Value = ""
  Me.E1GExpiryDate.Value = ""
  Me.E1GBoxPcs.Value = ""
  Me.E1GAmmount.Value = ""
  Me.E1Gkonz.Value = ""
  Me.E1GUnit.Value = ""

Call GetData

End Sub

1 个答案:

答案 0 :(得分:3)

  1. Date等同于Format
  2. 但更重要的是,看起来像日期的格式化文本Date的结果)与实际日期(数值)不同输入一个单元格,然后可以对其进行格式化。
  3. 因此,将NumberFormat写入单元格,然后更改单元格的.... .Cells(lRow, 6).Value = Date .Cells(lRow, 6).NumberFormat = "mmm.yyyy" ....

在您的情况下:

{{1}}