Format date in yyyy-mm-dd in cell

In [excel-vba], I am trying to format text from a UserForm text box, format it to yyyy-mm-dd and enter it in a cell with the following code:

Private Sub GenerateButton_Click()
    Worksheets("Sheet1").Activate

    Sheet1.Unprotect
    Dim startTime, endTime, startDate
    startTime = t_daylasthour.Text
    endTime = t_daylasthour.Text
    startDate = t_startdate.Value

    Dim counter As Integer
    counter = 1

    Do
        Set field = ActiveWorkbook.Sheets("Sheet1").Cells(counter + 1, 1)
        field.Value = Format(t_startdate.Text, "yyyy-mm-dd") 
        counter = counter + 1
    Loop While counter < 10

End Sub

The output in the cell continues to exit as yyyy / mm / dd. Any other format I'm trying to use in my code works. For example, I tried to use yyyy-mm-dd, and it works, but every time I try to use the format specified in the code above, it is not.

Any ideas on what I'm doing wrong?

+4
source share
1 answer

, . Range.NumberFormat, , .

Do
    Set field = ActiveWorkbook.Sheets("Sheet1").Cells(counter + 1, 1)
    field = CDate(t_startdate.Text) 
    field.NUMBERFORMAT = "yyyy-mm-dd"
    counter = counter + 1
Loop While counter < 10

, Range.NumberFormat , General, Range.Value , -date.

Do
    Set field = ActiveWorkbook.Sheets("Sheet1").Cells(counter + 1, 1)
    field.NUMBERFORMAT = "@"
    field.Value = Format(t_startdate.Text, "yyyy-mm-dd") 
    counter = counter + 1
Loop While counter < 10
+3

All Articles