日期的循环

时间:2017-04-19 05:36:02

标签: excel-vba loops date vba excel

我试图通过开始日期和结束日期循环日期,例如它从4月3日开始到8月31日结束。4月3日之后,它将添加7天到结束日期并显示日期和月份。我试图循环它,但它循环不停,并没有停止。前两周有效但开始直到循环,它不起作用。在此先感谢您的帮助

x = 5
endD = Sheet1.Range("C20").Value
startD = Sheet1.Range("C19").Value
mth = Month(startD)
Name = MonthName(mth, True)
no = Day(startD)
Sheet1.Range("C2").Value = no
Sheet1.Range("C1").Value = Name
dates = DateAdd("d", 7, startD)
Name = MonthName(Month(dates), True)
Sheet1.Range("D2").Value = Name
Sheet1.Range("D1").Value = Day(dates)
W = dates
Do Until W = endD
m = DateAdd("d", 7, W)
W = m
no = 0
Name = " "
no = Day(m)
Name = MonthName(Month(m), True)
Sheet1.Cells(1, x).Value = Name
Sheet1.Cells(2, x).Value = no
x = x + 1
Loop

2 个答案:

答案 0 :(得分:2)

  

self.myCollectionView.removeObserver(self, forKeyPath: "contentSize")

这可能导致无限循环,因为变量Do Until W = endD的递增值可能"跳过" 越过endD。替换为:

W

答案 1 :(得分:0)

Sub test()

Dim FirstDate As Date
Dim LastDate As Date
Dim NextDate As Date

FirstDate = Range("startdate").Value
LastDate = Range("enddate").Value

NextDate = FirstDate
Range("tripdays").Select

Do Until NextDate > LastDate

    ActiveCell.Value = NextDate
    ActiveCell.Offset(1, 0).Select
    NextDate = NextDate + 1

Loop

End Sub
这是怎么回事?