虽然循环没有终止VBA

时间:2017-04-20 19:15:48

标签: excel excel-vba while-loop vba

这是部分代码。基本上我想运行一个循环,当totalHoursNeeded为0或更低,或者currentDate为6/30时,循环终止。变量totalHoursNeeded是一个全局变量,并在函数runReport中递增减少。在runReport期间,currentDate也是一个全局变量,也会被重新分配。

所以我们说currentDate初始化为3/1/2016,而totalHoursNeeded初始化为234. runReport将在200小时的基础上运行,将当前日期设置为5/5/2016,然后将totalHoursNeeded设置为34(200是每个报告可以运行的最大值)。然后,我想要做的是运行另一份报告,但不是200和3/1/2016,我希望它是34和5/5/2016。

问题是这些报告无法通过2016年6月30日;例如,如果我需要运行基于200小时的报告,并且开始日期是2016年6月23日,则结束日期必须是2016年6月30日,并且报告中的小时数需要相应减少

无论如何,下面的while循环不会终止

Dim currentDate As Date
Dim totalHoursNeeded As Long
Dim totalHoursInExtension As Long
Dim hoursPerDay As Long


 While totalHoursNeeded > 0 Or Not (Month(currentDate) = 6 And Day(currentDate) = 30)

            'make a new word document object
            Dim nWord As New Document

            Set nWord = Documents.Open("c:\document\here", Visible:=False)

            'run and save the report
            On Error GoTo errhandler:

            'if the extensions have not been written through 6/30 of any year
            If Not (Month(currentDate) = 6) And Not (Day(currentDate) = 30) Then
                'run a report based on the current row, two worksheets, and word object
                totalHoursNeeded = totalHoursNeeded - runReport(row, summary, oWorksheet, nWord)
                saveReport row, totalHoursInExtension, oWorksheet, nWord
            Else

            End If
        '   Close things
            nWord.Close False

    Wend

0 个答案:

没有答案