days in month

T

tamar

hi
I have a question in visual basic
which function should I use to retrive the number of days in a specific month
thank you for your help
Tamar.
 
K

Ken Snell

Here's a function that will do this (you send the "number" of the month and
the "year" to the function):



Public Function DaysInAMonth(ByVal intMonthNumber As Integer, ByVal
intYearValue As Integer) As Integer
If intMonthNumber < 0 Or intMonthNumber > 12 Then
DaysInAMonth = 0
Else
If intMonthNumber = 12 Then intMonthNumber = 0
DaysInAMonth = Day(DateSerial(intYearValue, intMonthNumber + 1, 0))
End If
End Function


To use this function, put it in a regular module (the module must be named
something other than the name of the function) and then you can call this
function this way to get the number of days in January 2004:

DaysMonth = DaysInAMonth(1, 2004)
 
C

Cheryl Fischer

This works in VBA:

Format(DateSerial(Year(Date()), Month(Date())+1, 0),"d")


hth,
 
M

Marshall Barton

Ken said:
Here's a function that will do this (you send the "number" of the month and
the "year" to the function):



Public Function DaysInAMonth(ByVal intMonthNumber As Integer, ByVal
intYearValue As Integer) As Integer
If intMonthNumber < 0 Or intMonthNumber > 12 Then
DaysInAMonth = 0
Else
If intMonthNumber = 12 Then intMonthNumber = 0
DaysInAMonth = Day(DateSerial(intYearValue, intMonthNumber + 1, 0))
End If
End Function


Just a FYI, there is no need to check if intMonthNumber=12

DateSerial has no problem understanding any combination of
numbers for year, month and day that result in a date in the
range 1/1/100 through 12/31/9999
 
K

Ken Snell

Marshall Barton said:
Just a FYI, there is no need to check if intMonthNumber=12

DateSerial has no problem understanding any combination of
numbers for year, month and day that result in a date in the
range 1/1/100 through 12/31/9999

Now that you post this, I do recall that it works ok with 13 as the month
number ... thanks for the reminder!
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Top