简体   繁体   中英

How can I transfer an input text as a date to an Excel cell in VBA

when I transfer the row, the error wrong data type or user-defined data type is always displayed even though the cell is defined as a date. So apparently not the date but a string is transmitted. How can I pass the string txtEnd as a date. I tried it, but it didn't work.

Thank you for your help.

Best regards

Private Sub CommandButton1_Click()
    Dim iRow As Long
    
    iRow = Tabelle1.Range("C" & Rows.Count).End(xlUp).Row + 1
    With Tabelle1
        .Range("C" & iRow).Value = Me.txtTask.Value
        .Range("D" & iRow).Value = Me.txtDescription.Value
        .Range("E" & iRow).Value = "important"
        .Range("F" & iRow).Value = "open"
        .Range("G" & iRow).Value = "not started"
        .Range("H" & iRow).Value = "not started"
        .Range("I" & iRow).Value = Me.txtBegin.Value
        .Range("J" & iRow).Value = Me.txtEnd.Value
        .Range("K" & iRow).Value = "0 %"
    End With
    
    With Me
        For Each ctl In .Controls
            If TypeName(ctl) = "TextBox" Then
                ctl.Value = vbNullString
            End If
        Next
    End With
End Sub
    
Private Sub txtEnd_AfterUpdate()
    On Error Resume Next
    Me.txtEnd = CDate(Me.txtEnd)
End Sub

If you want the value from txtEnd to be entered in column J as a date use DateValue

.Range("J" & iRow).Value = DateValue(Me.txtEnd.Value)

That should give you a date value in column J and you can format it as required.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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