2014-02-18 104 views
0

我几乎完成了这段代码,它应该允许我计算2个日期范围中的星期二,星期四,星期六和星期日的数量。突出显示“skips = ModWeekdays(NotificationDate,OrderDate,PlacementDate,ReleaseDate)”行,错误框中显示错误“28”,堆栈空间不足。有人能帮助我吗?堆栈空间不足错误

'//////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 While NotificationDate <= OrderDate 
    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 
    NotificationDate = DateAdd("d", 1, NotificationDate) 
    Loop 

    Do While PlacementDate <= ReleaseDate 
    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 = WeekendDays2 + 1 
    End If 
    PlacementDate = PlacementDate + 1 
    Loop 

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


End Function 
+3

为什么你总是再次调用你的函数? – SLaks

+0

,因为我想导出要访问的函数的值。我想我会使用变量“跳过”来做这个 – NavyNuke

+0

如果你需要从函数返回值,那么赋值语句是向后的。尝试:'ModWeekdays =跳过'而不是'skips = ModWeekdays(NotificationDate,...') –

回答

3

我很困惑。这个递归函数实际上在什么时候结束?在它你具备以下条件:

skips = ModWeekdays(NotificationDate, OrderDate, PlacementDate, ReleaseDate) 

我不能为我的在什么时候会停止这样做生活工作(我怀疑从来没有)。我认为发生的事情是你一直在不停地调用函数,最终你的空间不足,因此你的错误。

+0

嗯,我的困惑来自事实,我试图让功能等于WeekendDays + WeekendDays2。我如何将这笔钱汇出来进入? – NavyNuke

+1

我想我会采取不同的方式。在你的函数之外有一个循环(也许在你的主要方法中),它循环每周你正在寻找的值。然后调用上面的函数获取值并将它们全部存储在主函数中并返回结果(取出递归!)。这样你就可以保证它最终会结束。 – Andrew

相关问题