Solved

Formula to find the last day of the month

Posted on 2000-04-10
6
164 Views
Last Modified: 2010-05-02
I need to find the last day of the current month (it could be any month).
0
Comment
Question by:rashida
6 Comments
 
LVL 4

Expert Comment

by:arcusd
ID: 2702694
Function MonthLastDay(mdate As Variant) As Variant
    On Error Resume Next
    Dim thismo, nextmo, thisyr, nextyr As Integer
    Dim mstr As String
    thismo = Month(mdate)
    thisyr = Year(mdate)
    If thismo = 12 Then
        nextmo = 1
        nextyr = thisyr + 1
    Else
        nextmo = thismo + 1
        nextyr = thisyr
    End If
    mstr = Trim(Str(nextmo)) + "/01/" + Trim(Str(nextyr))
    MonthLastDay = DateAdd("d", -1, CDate(mstr))
End Function


to call:

x = MonthLastDay(#12/01/1999#)
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2702697
Dim dDate As Date
dDate = #2/2/00#
Debug.Print Day(DateSerial(Year(dDate), Month(dDate) + 1, 0))
0
 
LVL 32

Expert Comment

by:Erick37
ID: 2702771
For current month:

Debug.Print Day(DateSerial(Year(Date), Month(Date) + 1, 0))
0
How to run any project with ease

Manage projects of all sizes how you want. Great for personal to-do lists, project milestones, team priorities and launch plans.
- Combine task lists, docs, spreadsheets, and chat in one
- View and edit from mobile/offline
- Cut down on emails

 
LVL 142

Accepted Solution

by:
Guy Hengel [angelIII / a3] earned 25 total points
ID: 2703232
To find the LAST day of the current month, use this:
DateAdd("d", -1, DateAdd("m", 1, DateSerial(Year(Now), Month(Now), 1)))
(you may replace Now by the date you want)
0
 
LVL 1

Expert Comment

by:jannea
ID: 2703596
Function LastDay(ByVal XDate As Variant) As Variant
    If Not IsDate(XDate) Then MsgBox "Error", 16: Exit Function
    XDate = Format$(XDate, "yyyy-mm-dd")
    XDate = Left$(XDate, 8) & "01"
    XDate = DateAdd("d", -1, XDate)
    LastDay = DateAdd("m", 1, XDate)
End Function
0
 

Author Comment

by:rashida
ID: 2703768
This was the easiest of all.

Thanks!!
0

Featured Post

How your wiki can always stay up-to-date

Quip doubles as a “living” wiki and a project management tool that evolves with your organization. As you finish projects in Quip, the work remains, easily accessible to all team members, new and old.
- Increase transparency
- Onboard new hires faster
- Access from mobile/offline

Join & Write a Comment

Background What I'm presenting in this article is the result of 2 conditions in my work area: We have a SQL Server production environment but no development or test environment; andWe have an MS Access front end using tables in SQL Server but we a…
You can of course define an array to hold data that is of a particular type like an array of Strings to hold customer names or an array of Doubles to hold customer sales, but what do you do if you want to coordinate that data? This article describes…
Get people started with the utilization of class modules. Class modules can be a powerful tool in Microsoft Access. They allow you to create self-contained objects that encapsulate functionality. They can easily hide the complexity of a process from…
This lesson covers basic error handling code in Microsoft Excel using VBA. This is the first lesson in a 3-part series that uses code to loop through an Excel spreadsheet in VBA and then fix errors, taking advantage of error handling code. This l…

747 members asked questions and received personalized solutions in the past 7 days.

Join the community of 500,000 technology professionals and ask your questions.

Join & Ask a Question

Need Help in Real-Time?

Connect with top rated Experts

9 Experts available now in Live!

Get 1:1 Help Now