我有两个文本框,分别包含开始日期和结束日期。
我想获得一个数字,代表这两者之间的日期。
Private Sub TextBox20_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'check date format
With TextBox20
If IsDate(TextBox20.Text) Then 'Format as desired.
TextBox20.Text = Format(CDate(TextBox20.Text), "Dddd, d Mmm yyyy", vbMonday)
Else
TextBox20.Text = "" 'Clear the TextBox
MsgBox "Por favor, ingresar una fecha valida."
Cancel = True
Exit Sub
End If
End With
End Sub
Private Sub TextBox21_Exit(ByVal Cancel As MSForms.ReturnBoolean)
'check date format
With TextBox21
If IsDate(TextBox21.Text) Then 'Format as desired.
TextBox21.Text = Format(CDate(TextBox21.Text), "Dddd, d Mmm yyyy", vbMonday)
Else
TextBox21.Text = "" 'Clear the TextBox
MsgBox "Por favor, ingresar una fecha valida."
Cancel = True
Exit Sub
End If
End With
End Sub
Private Sub TextBox22_AfterUpdate()
TextBox22.Value = DateDiff("d", CDate(TextBox20.Value), CDate(TextBox21.Value), vbMonday)
End Sub
我以为是因为我将TextBoxes设为.Text
而不是.Value
,但没有任何变化。
根据文本框字符串正确计算DateDiff
虽然我通常do建议不要在文本框中将overwrite原始控制项(日期)建议为重新格式化(日期)的字符串,但我展示了一种非常接近您的方法的可能方法。这包括一些有关可读日期字符串的规定,以允许进行DateDiff()
计算。
UserForm代码模块的声明头
为了保留正确的开始日期和结束日期(作为Date
类型,而not作为字符串),我定义了一个Type
结构,可以在后续过程中轻松地重用它。
Option Explicit
Private Type TThis
StartDate As Date
EndDate As Date
End Type
Dim this As TThis
示例过程
Sub CheckDays()
根据上面声明的类型结构中的条目计算DateDiff
。它与您的代码不同,因为每次退出文本框输入后都会调用计算函数。 Function getDateString()
试图通过调整格式设置结果使TextBox字符串可转换为Date类型,这将导致错误的日期转换。在调用IsDate()
计算之前,请在文本框的事件过程中进行最后的CheckDays
检查。Private Sub CheckDays()
' Purpose: calculate DateDiff and enter result into TextBox22
' Note : called by TextBox20_Exit|TextBox21_Exit events
If this.StartDate > 0 And this.EndDate >= this.StartDate Then
TextBox22.Text = DateDiff("d", this.StartDate, this.EndDate, vbMonday)
End If
End Sub
Private Function getDateString(ByVal txt As String) As String
' Purpose: make (TextBox) string convertible to Date type, if possible
'a) remove leading day names (e.g. "Wednesday, 1 Jan 2020" ~> "1 Jan 2020")
If InStr(txt, ",") > 0 Then txt = Split(txt, ",")(1)
'b) add current year if missing (e.g. "1.1." ~> "1.1.2020")
If Not IsDate(txt) Then
If IsDate(txt & " " & Year(Now)) Then txt = txt & " " & Year(Now)
End If
getDateString = txt ' return function result
End Function
UserForm事件过程
这些过程几乎保持不变,但包括
DateString
赋值,以提供可转换的日期字符串以及CheckDays
的过程调用以计算DateDiff
并将结果输入TextBox22
。附加说明:代替_Exit
事件,也可以使用_AfterUpDate
事件。
Private Sub TextBox20_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With TextBox20
Dim DateString As String
DateString = getDateString(.Text) 'Make TextBox string convertible to Date type.
'check date format
If IsDate(DateString) Then 'Format as desired.
this.StartDate = CDate(DateString) 'Remember date (Date type).
.Text = Format(this.StartDate, "Dddd, d Mmm yyyy", vbMonday)
'========
CheckDays '<< Call procedure to calculate DateDiff.
'--------
Else
.Text = "" 'Clear the TextBox string
this.EndDate = 0
MsgBox "Por favor, ingresar una fecha valida."
Cancel = True
Exit Sub
End If
End With
End Sub
Private Sub TextBox21_Exit(ByVal Cancel As MSForms.ReturnBoolean)
With TextBox21
Dim DateString As String
DateString = getDateString(.Text) 'Make TextBox string convertible to Date type.
'check date format
If IsDate(DateString) Then 'Format as desired.
this.EndDate = CDate(DateString) 'Remember date (Date type).
.Text = Format(this.EndDate, "Dddd, d Mmm yyyy", vbMonday)
'========
CheckDays '<< call procedure to calculate DateDiff
'--------
Else
.Text = "" 'Clear the TextBox string
this.EndDate = 0
MsgBox "Por favor, ingresar una fecha valida."
Cancel = True
Exit Sub
End If
End With
End Sub