Want to protect your cyber security and still get fast solutions? Ask a secure question today.Go Premium

x
  • Status: Solved
  • Priority: Medium
  • Security: Public
  • Views: 197
  • Last Modified:

Incrementing dates

I'm just trying to create a loop that will increment a date by a day each time:

TestDate = "3/27/2005"
For x = 1 To 7
    Debug.Print TestDate + Day(x)
Next x

Seems pretty simple... Here's the results:

4/27/2005
3/28/2005
3/29/2005
3/30/2005
3/31/2005
4/1/2005
4/2/2005

See the first date?  What's up with that?  Why would +day(1) increment the MONTH?  Why does the month then decrease by one?  I don't get this...

So I try this:

TestDate = "3/27/2005"
For x = 1 To 7
    TestDate = TestDate + Day(x)
    Debug.Print TestDate
Next x

Results:

4/27/2005
4/28/2005
4/30/2005
5/3/2005
5/7/2005
5/12/2005
5/18/2005

???

Why does this make no sense whatsoever?
Thanks!
0
MIKEV
Asked:
MIKEV
  • 2
1 Solution
 
MIKEVAuthor Commented:
Oh, I can see my error in the second example above, it's adding 1 days, then 2 days, etc.  This is better:

TestDate = "3/27/2005"
For x = 1 To 7
    TestDate = TestDate + Day(1)
    Debug.Print TestDate
Next x

4/27/2005
5/28/2005
6/28/2005
7/29/2005
8/29/2005
9/29/2005
10/30/2005

Now it's inreasing the month?
0
 
Ryan ChongCommented:
try:

TestDate = CDate("3/27/2005")
    For x = 1 To 7
        TestDate = TestDate + 1
        Debug.Print TestDate
    Next x
0
 
Ryan ChongCommented:
or this if you using DateSerial, DateAdd functions:

TestDate = DateSerial(2005, 3, 27)
    For x = 1 To 7
        TestDate = DateAdd("d", 1, TestDate)
        Debug.Print TestDate
    Next x
0

Featured Post

Technology Partners: We Want Your Opinion!

We value your feedback.

Take our survey and automatically be enter to win anyone of the following:
Yeti Cooler, Amazon eGift Card, and Movie eGift Card!

  • 2
Tackle projects and never again get stuck behind a technical roadblock.
Join Now