VBA - Work out How Many Days are in a Month
On Apr 3, 10:06*pm, Mike H wrote:
Ah you said VBA
Public Function DaysInMonth(myMonth As Long) As Long
* * DaysInMonth = Day(DateSerial(Year(Date), myMonth + 1, 1) - 1)
End Function
Call with =DaysInMonth(Month(a1))
with a date in A1
Mike
"NateBuckley" wrote:
Just wondering if anyone has any idea how to find out how many days are in a
given month from a given year?
So If I were to put the month as APRIL 2008 I'd get back 30.
Thanks in advance!- Hide quoted text -
- Show quoted text -
Hi,
Jusa another variation... If we read your post literally, this version
of the function should handle a literal string as well. Actually, it
should handle pretty much anything. If you feed it something that
can't possibly be recognised as some sort of date, it will just give a
#VALUE error:
Function daysinMth(MonthYr As Variant)
If VarType(MonthYr) = vbString Then _
MonthYr = DateValue("1 " & MonthYr)
daysinMth = 32 - Day(MonthYr - Day(MonthYr) + 32)
End Function
Cheers,
Ivan.
|