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. All works fine but i have problems with date format.

I use that macro to set it to "wanted" format in UserForm textbox:

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 answer

  • answered 2018-10-11 19:08 BigBen

    1. DateSerial(Year(Date), Month(Date), Day(Date)) is equivalent to 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.
    3. So write the Date into the cell and then change the cell's NumberFormat.

    In your case:

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