Solved

Formula to find the last day of the month

Posted on 2000-04-10
6
167 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
Free Tool: Path Explorer

An intuitive utility to help find the CSS path to UI elements on a webpage. These paths are used frequently in a variety of front-end development and QA automation tasks.

One of a set of tools we're offering as a way of saying thank you for being a part of the community.

 
LVL 143

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

Announcing the Most Valuable Experts of 2016

MVEs are more concerned with the satisfaction of those they help than with the considerable points they can earn. They are the types of people you feel privileged to call colleagues. Join us in honoring this amazing group of Experts.

Question has a verified solution.

If you are experiencing a similar issue, please ask a related question

Have you ever wanted to restrict the users input in a textbox to numbers, and while doing that make sure that they can't 'cheat' by pasting in non-numeric text? Of course you can do that with code you write yourself but it's tedious and error-prone …
Since upgrading to Office 2013 or higher installing the Smart Indenter addin will fail. This article will explain how to install it so it will work regardless of the Office version installed.
As developers, we are not limited to the functions provided by the VBA language. In addition, we can call the functions that are part of the Windows operating system. These functions are part of the Windows API (Application Programming Interface). U…
Get people started with the process of using Access VBA to control Excel using automation, Microsoft Access can control other applications. An example is the ability to programmatically talk to Excel. Using automation, an Access application can laun…

839 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