为什么我会出现溢出错误?????VBA/ACCESS

为什么我会出现溢出错误?????VBA/ACCESS,vba,ms-access,Vba,Ms Access,请帮我找出我做错了什么。我在突出显示“weekendDays=weekendDays+1”部分时遇到溢出错误。本代码的目的是计算两个日期范围内的周二、周四、周六和周日的数量 '//////This is for Valley Estimate of Demurrage Days///////////// Public Function ModWeekdays(ByRef NotificationDate As Date, ByRef OrderDate As Date, ByRef Plac

请帮我找出我做错了什么。我在突出显示“weekendDays=weekendDays+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
   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

您没有递增变量,因此处于无限循环中。这将修正我们评论中的一些前后矛盾

您的函数应该如下所示:

'//////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)
然后,您可以从按钮(或运行此函数所做的任何操作)调用它,如下所示:

'//////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)

您的循环似乎是infinte,因为您从未修改过
NotificationDate
。在上面的代码中,它将永远不会等于
OrderDate
(除非它们恰好已经相同)。您不会更改
NotificationDate=OrderDate
中的任何一个,因此循环将永远运行,递增一个整数直到溢出?我怎么会忘记……thanksDateAdd(“d”,1,NotificationDate)=通知日期这行吗?你有没有像我上面所说的那样试着把它们翻过来?在您的公式中,您试图首先向变量添加一天,而在我的公式中,您将变量设置为等于DateAdd语句。实际上,在进行建议的更正后,我仍然会溢出OK,问题是最后一行再次调用函数,而这一次NotificationDate已经等于OrderDate。因此,第一次运行将NotificationDate递增为OrderDate+1,循环继续无限大。你为什么需要最后一行?我在网站上的另一个问题中被告知要补充这一点。我想这是一种将最终天数“导出”到access应用程序的方法。尝试删除它,我认为您应该可以。它正上方的一行“skips=WeekendDays+WeekendDays2”将为您提供价值。如果要返回该值,请将其更改为“ModWeekdays=WeekendDays+WeekendDays2”,并通过放置类似“X=ModWeekdays(日期1、日期2、日期3、日期4)”的内容来运行整个过程。这样,“X”将设置为函数最后一行的ModWeekdays。我知道这有点含糊不清,希望你听了我的话。