简体   繁体   English

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

[英]Transfer UserForm textbox.value as date format to sheet

What i'm doing wrong. 我做错了。 I want just to save my date from UserForm to the end of the list along with all other data. 我只想将日期和所有其他数据一起从UserForm保存到列表的末尾。 All works fine but i have problems with date format. 一切正常,但我有日期格式问题。

I use that macro to set it to "wanted" format in UserForm textbox: 我使用该宏在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

And that macro to transfer it: 和那个宏来转移它:

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. DateSerial(Year(Date), Month(Date), Day(Date)) is equivalent to Date . DateSerial(Year(Date), Month(Date), Day(Date))等同于Date
  2. But more importantly, formatted-text-that-looks-like-a-date (the result of Format ) is not the same as an actual date (a numerical value) entered into a cell, which can then be formatted. 但是更重要的是, 看起来像日期的格式化文本Format的结果)与输入到单元格中的实际日期(数值)不同,然后可以对其进行格式化。
  3. So write the Date into the cell and then change the cell's NumberFormat . 因此,将Date写入单元格,然后更改单元格的NumberFormat

In your case: 在您的情况下:

....
.Cells(lRow, 6).Value = Date
.Cells(lRow, 6).NumberFormat = "mmm.yyyy"
....

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM