2017-03-16 56 views
0

我無法比較日期。我的代碼中的if語句表示endDate不會少,即使它是。例如,如果endDate是2/20/2017和startDate 2/22/2017 if語句說endDate不是唯一的,但它是。訪問VBA比較日期不起作用

Dim startDate As Date 
    Dim endDate As Date 

     startDate = DateValue(Me.dueDateTxt) 
     endDate = DateValue(Me.shippedDate) 

     If (endDate < startDate) Then 
      Debug.Print "It is less" 
     Else 
      Debug.Print "not less" 
     End If 

我也曾嘗試

If Me.dueDateTxt < Me.ShippedDate Then 
    If CDate(startDate) < CDate(endDate) Then   
    If Format(startDate, "mm/dd/yyyy") < Format(endDate, "mm/dd/yyyy") Then 
    If DateDiff(d, startDate, endDate) > 0 Then 

我失去了一些東西的地方預先感謝您的幫助!

編輯:我找出了問題所在。我有一個功能來消除假期和週末。該函數正在交換我的startDate和endDate。感謝大家的幫助和建議。

+0

您是否嘗試過國際日期,yyyy-mm-dd? – Fionnuala

+0

我還沒有嘗試過。從文本框中實現的日期以mm/dd/yyyy顯示。在比較它們之前,我需要更改格式嗎? – Kevin

+0

在VBA中,使用國際日期格式總是一個好主意。你可以使用FORMAT來獲得這個。 – Fionnuala

回答

1

你的第一塊代碼對我來說工作的很好。

您是否將文本框(?)的格式設置爲「Short Date」?

+0

我想清楚發生了什麼事。我有一個功能來消除假期和週末。該函數正在交換我的startDate和endDate。令人沮喪的是,它花了所有的時間來找到它。感謝您的回覆和建議。 – Kevin