我正在为员工设置用户表格以填写数据。他们需要填写日期,例如DD-MM-YYYY,输出也必须是DD-MM-YYYY。但是我使用的代码将输入5-12-2019(DD-MM-YYYY)更改为输出12-5-2019(MM-DD-YYYY)。在用户表单中,将其传输到Excel工作表时,其预期效果与(DD-MM-YYYY)相同。如果月份大于13,则不会更改,因此输入13-12-2019(DD-MM-YYYY)保持输出13-12-2019(DD-MM-YYYY)。代码怎么可能会更改输出,但不是每次都以相同的方式更改,我在代码中做错了什么吗?
Private Sub TextBox1_BeforeUpdate(ByVal cancel As MSForms.ReturnBoolean)
If IsDate(Me.TextBox1.Text) Then
Me.TextBox1.Text = Format(Me.TextBox1.Text, "DD-MM-YYYY")
Else
MsgBox "Vul een geldige datum in", vbRetryCancel + vbCritical
End If
End Sub
Public Function IsTime(Expression As Variant) As Boolean
If IsDate(Expression) Then
IsTime = (Int(CSng(CDate(Expression))) = 0)
End If
End Function
答案 0 :(得分:1)
5-12-2009
。-
分割字符串。DateSerial()
获得一个功能齐全的日期。Public Function StringToDate(myInput As String) As Date
Dim day As Long
Dim month As Long
Dim year As Long
Dim dateArray As Variant
dateArray = Split(myInput, "-")
day = dateArray(0)
month = dateArray(1)
year = dateArray(2)
StringToDate = DateSerial(year, month, day)
End Function
Public Sub Main()
Debug.Print month(StringToDate("05-10-2001"))
Debug.Print StringToDate("05-10-2001")
End Sub