2017-01-30 42 views
0

基本設置是,有2個文本框和1個日曆(Datepicker)。文本框宏在日曆上添加或減去日期和選擇日期(Datepicker)

在textbox1中,用戶輸入下面提到的任何一種格式的日期,按回車,日期 在日曆上得到選擇

03月

03月17

03月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 

目前正在發生的事情是 -

用戶輸入需要在文本框2中添加或減少的天數,按回車,出現一個消息框 ,顯示最終結果。

而不是一個消息框,我只是想要代碼選擇在日曆上的最終結果。

我不確定如何更改textbox2代碼來實現此目的。

請協助。

注:就像TextBox1中,其中代碼選擇在日曆上的日期,我想TextBox2中的代碼做同樣的,這是在日曆中選擇天數相加或相減後日期。

+0

'monthStr =月(現在)'不會返回一個有效的月份縮寫。您可能想使用'Format(Now,「mmm」)來代替,但請注意這是區域設置敏感的。 –

回答

0

替代:

MsgBox Format(CDate(d) + Val(TextBox2.Value), fmt) 

有:

Me.Calendar1.Value = CDate(d) + Val(TextBox2.Value)