Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations Mike Lewis on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

DateAdd-Start with last day of February + 1 month

Status
Not open for further replies.

SiouxCityElvis

Programmer
Jun 6, 2003
228
0
0
US
I am trying to get the following For Loop to return the last day of each month starting with the last day in February. But when I start with a date of 02/28/2006 or 02/29/2008 the rest of the months end up with '28' as their day value. I need it to be the last day of the month for each month.
[
For I = 0 to UBound(varB)

ArrayA(I).dtBillDate = Date2Num(DateAdd("m", I * globStruct.iFrqCnt, Num2Date(globStuct.dtBDate)))

Next
]

Its been a while since I posted a thread - hope I put the code snippet in correctly - can't remember the special chars to surround the code snippet with.
Thanks.
 
Waht if instead you move through finding the first of each month and then subtracting 1 day?
 
Sheco,

Thanks.
I used something that Bob suggested in thread222-97014 which is basically what you are saying.

I return a long since we store our dates as long.

Code:
For I = 0 To UBound(OfSomething)
...
arrayOne(I).longDateVar = Date2Num(DateAdd("m", I * blah.iFreqCnt, Num2Date(blah.dtFirstDate)))

--If Feb and either 28th or 29th--
arrayOne(I).longDateVar = CheckTheDate(Num2Date(arrayOne(I).longDateVar))
Next
...
Private Function CheckTheDate(BillDate As Date) As Long
  CheckTheDate = DateAdd("d", -1, DateAdd("m", 1, DateAdd("d", 1 - DatePart("d", Date2Num(BillDate)), Date2Num(BillDate))))
End Function

This code got me the last day of the month for an array of months when the 9556 (2/28/2006) was the date stored in the blah.dtFirstDate field. So, when I have 10 payments to schedule with a frequency of 1, I get 02/28/2006, 03/31/2006, 04/30/2006, etc.

Thanks.
-David
 
I'd look at MichaelRed's solution in that thread rather than Bob's. MichaelRed's is somewhat more efficient.
 


Simple!

The LST day of any month is
Code:
DateSerial(TheYear, TheMonth+1, 0)

Skip,
[sub]
[glasses] [red]Be Advised![/red] Dyslexic poets...
write inverse! [tongue][/sub]
 
Which is what MichaelRed's suggested in the linked thread.
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top