ExcelBanter

ExcelBanter (https://www.excelbanter.com/)
-   Excel Programming (https://www.excelbanter.com/excel-programming/)
-   -   Days in a month Function (https://www.excelbanter.com/excel-programming/331176-days-month-function.html)

Marek

Days in a month Function
 
Hi,

Does anyone know how to work out how many days a month has? Something that
can accomodate leap years as well. So, if it's Feb 2005 it's 28 days. If it
were Feb 2004 it would be 29 days.


--
Many thanks in advance for any assistance

Steve Yandl[_3_]

Days in a month Function
 
If some date in that month is given in cell A1

=DAY(DATE(YEAR(A1),MONTH(A1)+1,0))


Steve


"Marek" wrote in message
...
Hi,

Does anyone know how to work out how many days a month has? Something
that
can accomodate leap years as well. So, if it's Feb 2005 it's 28 days. If
it
were Feb 2004 it would be 29 days.


--
Many thanks in advance for any assistance




Marek

Days in a month Function
 
Thanks - gives me a clue. How could I do this purely from code - I don't
have a cell reference for the date.
--
Many thanks in advance for any assistance


"Steve Yandl" wrote:

If some date in that month is given in cell A1

=DAY(DATE(YEAR(A1),MONTH(A1)+1,0))


Steve


"Marek" wrote in message
...
Hi,

Does anyone know how to work out how many days a month has? Something
that
can accomodate leap years as well. So, if it's Feb 2005 it's 28 days. If
it
were Feb 2004 it would be 29 days.


--
Many thanks in advance for any assistance





George Nicholson[_2_]

Days in a month Function
 
In VB, the last day of any month is day 0 of the following month (i.e.,
First of next month, less one day)

NumDaysInMyDate = Day(DateSerial(Year(MyDate), Month(MyDate)+1, 0))

The DateSerial(year,month,day) function calculates "Day 0" of the month
following MyDate. Equivalent to MyDate's EOM.
The Day(datevalue) function returns the number (1-31) of that day (i.e., the
number of days within the month of MyDate)

Leap years are covered.

HTH,
--
George Nicholson

Remove 'Junk' from return address.


"Marek" wrote in message
...
Hi,

Does anyone know how to work out how many days a month has? Something
that
can accomodate leap years as well. So, if it's Feb 2005 it's 28 days. If
it
were Feb 2004 it would be 29 days.


--
Many thanks in advance for any assistance




JE McGimpsey

Days in a month Function
 
One way:


Public Function DaysInMonth(vDate As Variant) As Variant
Dim nDaysInMonth As Long
On Error GoTo ErrHandler
nDaysInMonth = Day(DateSerial(Year(vDate), Month(vDate) + 1, 0))
DaysInMonth = nDaysInMonth
Continue:
On Error GoTo 0
Exit Function
ErrHandler:
DaysInMonth = CVErr(xlErrValue)
Resume Continue
End Function



In article ,
"Marek" wrote:

Thanks - gives me a clue. How could I do this purely from code - I don't
have a cell reference for the date.


Marek

Days in a month Function
 
that works a treat - many thanks for that. It never ceases to amaze me the
input on these forums and the speed folk can respond nowadays.


"George Nicholson" wrote:

In VB, the last day of any month is day 0 of the following month (i.e.,
First of next month, less one day)

NumDaysInMyDate = Day(DateSerial(Year(MyDate), Month(MyDate)+1, 0))

The DateSerial(year,month,day) function calculates "Day 0" of the month
following MyDate. Equivalent to MyDate's EOM.
The Day(datevalue) function returns the number (1-31) of that day (i.e., the
number of days within the month of MyDate)

Leap years are covered.

HTH,
--
George Nicholson

Remove 'Junk' from return address.


"Marek" wrote in message
...
Hi,

Does anyone know how to work out how many days a month has? Something
that
can accomodate leap years as well. So, if it's Feb 2005 it's 28 days. If
it
were Feb 2004 it would be 29 days.


--
Many thanks in advance for any assistance






All times are GMT +1. The time now is 09:46 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
ExcelBanter.com