The code above works like this:
In some cell, you have a date. Let's say it's A1. This date can be broken down into it's components (day, month, year) by using the built-in excel functions:
DAY(), YEAR(), MONTH().
Also, excel makes available the function called "DATE()" to build a date, if you have the three components. If you placed:
=DATE(2003, 1, 2)
in a cell on your worksheet, that cell would then become the date "January 2nd, 2003", depending on how you formatted it.
So, you can construct dates out of three components, using the DATE function.
This is all pulled together in the example given above, when you say:
=DATE(YEAR(A1), MONTH(A1) + 1, 0)
You're effectively saying:
* The year is the same as the YEAR() in cell A1.
* The month is one more than the month in cell A1.
* The day is zero.
Think about that last bit. What's January 0th, 2002?
To excel, the zero-th day of a month, is the last day of the PREVIOUS month. So January 0, 2002, is actually December 31, 2001.
After all that, you surround the entire formula with the DAY() function, in order to pull out just the 2 digit day. In the case of January 0, 2002, you'll pull out the 31 from December.
I've struggled with this same issue for some time. Calculating the end of the month is something we do quite often here with our spreadsheets, so here's a function that you can place in your VB project, so that you can just type:
=EndOfMonth(A1)
In a cell, and if A1 is a date, you'll get the last day of that date.
Place this code in a new module:<pre>
Function EndOfMonth(mDate As Date) As Variant
EndOfMonth = Day(DateSerial(Year(mDate), Month(mDate) + 1, 0))
End Function</pre>
_________________<form action="http://www.google.com/search" name=f>
<input style="border:1px solid;" maxLength=256 size=15 name=q value=""><input type=submit value="Search" name=btnG></form>
This message was edited by OdinsDream on 2003-01-06 14:09