Tek-Tips is the largest IT community on the Internet today!

Members share and learn making Tek-Tips Forums the best source of peer-reviewed technical information on the Internet!

  • Congratulations bkrike on being selected by the Tek-Tips community for having the most helpful posts in the forums last week. Way to Go!

Function LastDayOfMonth 1

Status
Not open for further replies.

EZLI

IS-IT--Management
Sep 3, 2002
31
LV
Hi,
I'm asking kindly for help.

Function FirstOfMonth()
FirstOfMonth = DateSerial(Year(Now),_ Month(Now), 1)

For Mart:

Function LastOfMonth()
LastOfMonth = DateSerial(Year(Now),_
Month(Now),31)

For different months the last day is different.
The Question;
How automaticly define the last day for different months?

Thank's in advance,
Niks
 
This may not be "optimal", but it works well.

Code:
Public Function LastOfMonth(Optional currentDate as Date = 0) as Date
    Dim nextMonth as Date

    If currentDate = 0 Then
        'Set the "default" value to today
        currentDate = Date()
    End If


    nextMonth = DateSerial(Year(currentDate),Month(currentDate)+1,1)
    LastOfMonth = CDate(nextMonth - 1)
End Function
 --
Find common answers using Google Groups:

[URL unfurl="true"]http://groups.google.com/groups?group=comp.databases.ms-access[/URL]
 
? DateSerial(Year(Date), Month(Date) + 1, 0)

or, perhaps more formally

LastDayOfMonth = DateSerial(Year(Date), Month(Date) + 1, 0)


You can, of course, use ANY valid date expression in place of Date in the above. You can also do as foolio12, and encapuslate it in a procedure, but that does incur some overhead,

MichaelRed
m.red@att.net

Searching for employment in all the wrong places
 
You also may use the Excel function "EOMONTH":

Sub LastOfMonth()
'For current month:
Cells(1, 1).Formula = "=EOMONTH(TODAY(),0)"
MsgBox Format(Cells(1, 1), "MMMM DD, YYYY")
Cells(1, 1).Clear
End Sub

For next month instead of "0" you will put 1, or -1 for the previous.....

Fane
 
Status
Not open for further replies.

Part and Inventory Search

Sponsor

Back
Top