请帮我辨别我做错了什么。我收到溢出错误,突出显示“weekendDays = weekenddays + 1”部分。此代码的目的是计算2个日期范围内的星期二,星期四,星期六和星期日的数量。
'//////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 :(得分: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)