2014-02-18 26 views
0

請幫我確定我做錯了什麼。我突出顯示了「週末日=週末日+ 1」部分的溢出錯誤。此代碼的目的是計算2個日期範圍內的星期二,星期四,星期六和星期日的數量。爲什麼我得到溢出錯誤????? VBA/ACCESS

'//////This is for Valley Estimate of Demurrage Days///////////// 
Public Function ModWeekdays(ByRef NotificationDate As Date, ByRef OrderDate As Date, ByRef PlacementDate As Date, ByRef ReleaseDate As Date) As Integer 
Dim skips As Integer 
Dim WeekendDays As Integer 
Dim WeekendDays2 As Integer 
'skips = 0 
WeekendDays = 0 
WeekendDays2 = 0 


    Do 
    If DatePart("w", NotificationDate) = 0 Then 
    WeekendDays = WeekendDays + 1 
    ElseIf DatePart("w", NotificationDate) = 2 Then 
    WeekendDays = WeekendDays + 1 
    ElseIf DatePart("w", NotificationDate) = 4 Then 
    WeekendDays = WeekendDays + 1 
    ElseIf DatePart("w", NotificationDate) = 6 Then 
    WeekendDays = WeekendDays + 1 
    End If 
    Loop Until NotificationDate = OrderDate 

    Do 
    If DatePart("w", PlacementDate) = 0 Then 
    WeekendDays2 = WeekendDays2 + 1 
    ElseIf DatePart("w", PlacementDate) = 2 Then 
    WeekendDays2 = WeekendDays2 + 1 
    ElseIf DatePart("w", PlacementDate) = 4 Then 
    WeekendDays2 = WeekendDays2 + 1 
    ElseIf DatePart("w", PlacementDate) = 6 Then 
    WeekendDays2 = WeekendDays + 1 
    End If 
    Loop Until PlacementDate = ReleaseDate 

    skips = WeekendDays + WeekendDays2 
    skips = ModWeekdays(NotificationDate, OrderDate, PlacementDate, ReleaseDate) 


End Function 
+0

你的循環似乎是無限的,因爲你從來沒有修改'NotificationDate'。在上面的代碼中,它永遠不會等於'OrderDate'(除非它們碰巧已經是相同的)。 –

+0

您不改變'NotificationDate = OrderDate'中的任何一個,以便循環永遠運行,遞增一個整數直到溢出? –

+0

我怎麼忘了....謝謝 – NavyNuke

回答

1

你沒有增加你的變量,所以你處於一個無限循環。這應該解決我們評論中的一些來回問題。

你的功能應該是這樣的:

'//////This is for Valley Estimate of Demurrage Days///////////// 
Public Function ModWeekdays(ByRef NotificationDate As Date, ByRef OrderDate As Date, ByRef PlacementDate As Date, ByRef ReleaseDate As Date) As Integer 
Dim skips As Integer 
Dim WeekendDays As Integer 
Dim WeekendDays2 As Integer 
'skips = 0 
WeekendDays = 0 
WeekendDays2 = 0 


    Do 
    If DatePart("w", NotificationDate) = 0 Then 
    WeekendDays = WeekendDays + 1 
    ElseIf DatePart("w", NotificationDate) = 2 Then 
    WeekendDays = WeekendDays + 1 
    ElseIf DatePart("w", NotificationDate) = 4 Then 
    WeekendDays = WeekendDays + 1 
    ElseIf DatePart("w", NotificationDate) = 6 Then 
    WeekendDays = WeekendDays + 1 
    End If 
    ' This is the increment line you were missing 
    NotificationDate = DateAdd("d", 1, NotificationDate) 
    Loop Until NotificationDate = OrderDate 

    Do 
    If DatePart("w", PlacementDate) = 0 Then 
    WeekendDays2 = WeekendDays2 + 1 
    ElseIf DatePart("w", PlacementDate) = 2 Then 
    WeekendDays2 = WeekendDays2 + 1 
    ElseIf DatePart("w", PlacementDate) = 4 Then 
    WeekendDays2 = WeekendDays2 + 1 
    ElseIf DatePart("w", PlacementDate) = 6 Then 
    WeekendDays2 = WeekendDays + 1 
    End If 
    ' This is the increment line you were missing 
    PlacementDate = DateAdd("d", 1, PlacementDate) 
    Loop Until PlacementDate = ReleaseDate 

    ' No need to set Skip equal to the sum, just set ModWeekdays equal to it 
    ' so you can return the value. 
    ' Also, remove that last line, which re-calls the function and causes an infinite loop 
    ModWeekdays = WeekendDays + WeekendDays2 

End Function 

然後,你會說它是從您的按鈕(或者無論你做什麼來運行這個功能)這樣的:

Dim TotalDaysToSkip as Integer 

TotalDaysToSkip = ModWeekdays(Date1, Date2, Date3, Date4) 
+0

您是否嘗試翻轉它們,如上所述?在你的公式中,你試圖首先給變量添加一天,在我的設置中,變量等於DateAdd語句。 –

+0

其實我仍然越來越溢出後提出您的建議更正 – NavyNuke

+0

好的,問題是最後一行再次調用該函數,這次NotificationDate已經等於OrderDate。因此,第一個runthrough將NotificationDate遞增爲OrderDate + 1,並且循環持續到無窮大。你爲什麼需要最後一行? –