基本设置是,有2个文本框和1个日历(Datepicker)。
在textbox1中,用户以下面提到的任一格式输入日期,按enter键,日期 在日历上获得选中。
03 - 2月
03-FEB-17
03-FEB-17
在textbox2中,用户输入需要添加或减去的天数,如下所示,按回车键, 日期在日历上被选中。
+1,+ 15,+ 32 ......等等以添加天数
-1,-12,-21 ......等等,减去天数
下面的Textbox1代码工作正常 -
Option Explicit
Private Sub TextBox1_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim txt As String, dayStr As String, monthStr As String, yearStr As String
Dim okTxt As Boolean
txt = Me.TextBox1.Value
Select Case Len(txt)
Case 2
dayStr = txt
okTxt = okDay(dayStr)
monthStr = Month(Now)
yearStr = Year(Now)
Case 5
dayStr = Mid(txt, 3, 3)
monthStr = Mid(txt, 3, 3)
okTxt = okDay(Left(txt, 2)) And okMonth(monthStr)
yearStr = Year(Now)
Case 7
dayStr = Mid(txt, 3, 3)
monthStr = Mid(txt, 3, 3)
yearStr = Mid(txt, 6, 2)
okTxt = okDay(Left(txt, 2)) And okMonth(monthStr) And okYear(yearStr)
End Select
If Not okTxt Then
MsgBox "Invalid date" _
& vbCrLf & vbCrLf & "Date must be input in one of the following formats:" _
& vbCrLf & vbTab & "dd" _
& vbCrLf & vbTab & "ddmmm" _
& vbCrLf & vbTab & "ddmmmyy" _
& vbCrLf & vbCrLf & "Please try again", vbCritical
Cancel = True
Else
Me.Calendar1.Value = CDate(Left(txt, 2) & " " & monthStr & " " & yearStr)
End If
End Sub
Function okDay(txt As String) As Boolean
okDay = CInt(txt) > 0 And CInt(txt) < 31
End Function
Function okMonth(txt As String) As Boolean
Const months As String = "JANFEBMARAPRMAJJUNJULAUGSEPOCTNOVDEC"
okMonth = InStr(months, UCase(txt)) > 0
End Function
Function okYear(txt As String) As Boolean
okYear = CInt(txt) > 0 And CInt(txt) < 200 '<--| set your "limit" years
End Function
下面的Textbox2代码是我需要帮助的地方 -
Private Sub TextBox2_Exit(ByVal Cancel As MSForms.ReturnBoolean)
Dim Ln As Variant
Dim x As Variant
Dim d As Variant
Dim fmt As Variant
If IsNumeric(Left(TextBox1, 2)) Then Ln = 0 Else Ln = 1
x = Left(TextBox2.Value, 1)
If x <> "-" And x <> "+" Then MsgBox "Please use an operator with your value":: Exit Sub
d = TextBox1.Value
Select Case Len(d)
Case 4, 5
d = Left(d, 2 - Ln) & "-" & Right(d, 3)
fmt = "ddmmm"
Case 6, 7
d = Left(d, 2 - Ln) & "-" & Mid(d, 3 - Ln, 3) & "-" & Right(d, Len(d) - (5 - Ln))
fmt = "ddmmmyy"
Case 8, 9
d = Left(d, 2 - Ln) & "-" & Mid(d, 3 - Ln, 3) & "-" & Right(d, Len(d) - (5 - Ln))
fmt = "ddmmmyyyy"
End Select
MsgBox Format(CDate(d) + Val(TextBox2.Value), fmt)
End Sub
目前正在发生的事情是 -
用户输入需要在textbox2中添加或减去的天数,按Enter键,输入消息框 出现显示最终结果。
我只想让代码在日历上选择最终结果,而不是消息框。
我不确定如何更改textbox2代码来实现此目的。
请帮忙。
注意:就像textbox1,代码选择日历上的日期一样,我希望textbox2代码执行相同的操作,这是选择日期后日历上的日期增加或减少。
答案 0 :(得分:0)
替代:
/Users/diiru/Desktop/Try/used_products/log/webpay
使用:
MsgBox Format(CDate(d) + Val(TextBox2.Value), fmt)