Reply
 
LinkBack Thread Tools Search this Thread Display Modes
  #1   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default VBA - Work out How Many Days are in a Month

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!
  #2   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default VBA - Work out How Many Days are in a Month

With a date in a1 try

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

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!

  #3   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 141
Default VBA - Work out How Many Days are in a Month

Credit to Ron Rosenfeld for this one... Here is a VBA implementation
of his worksheet function:

Sub daysinMth()
Dim WhichDate As Date
Dim Days As Integer

WhichDate = DateSerial(2008, 2, 12)

Days = 32 - Day(WhichDate - Day(WhichDate) + 32)

MsgBox Days & " in " & WhichDate
End Sub

PS. You did say you wanted a VBA version?

Cheers,
Ivan.

On Apr 3, 9:52*pm, Mike H wrote:
With a date in a1 try

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

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 -


  #4   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 11,501
Default VBA - Work out How Many Days are in a Month

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!

  #5   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 141
Default 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.


  #6   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default VBA - Work out How Many Days are in a Month

Thanks mate, both worked a treat, I shall have a play around.

Thank you.

"Ivyleaf" wrote:

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.

  #7   Report Post  
Posted to microsoft.public.excel.programming
external usenet poster
 
Posts: 146
Default VBA - Work out How Many Days are in a Month

Cheers matey I shall check it out now.

Thanks!

"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!

Reply
Thread Tools Search this Thread
Search this Thread:

Advanced Search
Display Modes

Posting Rules

Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On


Similar Threads
Thread Thread Starter Forum Replies Last Post
How many days in a row does an employee work within a 2 month peri LEG Excel Discussion (Misc queries) 2 December 16th 09 08:47 PM
excel to make the days cary over month to month automaticly GARY New Users to Excel 1 April 19th 08 06:05 PM
Number of days in month counted from shortened name of month & yea Tumar Excel Worksheet Functions 6 September 18th 07 03:36 PM
Days per month for calculating storage days Bart Excel Worksheet Functions 3 January 31st 07 06:40 PM
Number of work days/month Vanna Excel Worksheet Functions 1 March 16th 06 04:35 AM


All times are GMT +1. The time now is 08:56 PM.

Powered by vBulletin® Copyright ©2000 - 2024, Jelsoft Enterprises Ltd.
Copyright ©2004-2024 ExcelBanter.
The comments are property of their posters.
 

About Us

"It's about Microsoft Excel"