Is there a VBA solution for the Excel formula “=EOMonth(MyDate,0)”
Where the variable MyDate = 12/13/2009
The expected results would be 12/31/2009
Thanks,
John
![]() |
Patch reliability is unclear. Unless you have an immediate, pressing need to install a specific patch, don't do it. |
SIGN IN | Not a member? | REGISTER | PLUS MEMBERSHIP |
Home » Forums » AskWoody support » Productivity software by function » MS Excel and spreadsheet help » VBA Solution to EOMonth
I found something online:
Public Function EOMonth(InputDate As Date, Optional MonthsToAdd As Integer) ' Returns the date of the last day of month, a specified number of months ' following a given date. Dim TotalMonths As Integer Dim NewMonth As Integer Dim NewYear As Integer If IsMissing(MonthsToAdd) Then MonthsToAdd = 0 End If TotalMonths = Month(InputDate) + MonthsToAdd NewMonth = TotalMonths - (12 * Int(TotalMonths / 12)) NewYear = Year(InputDate) + Int(TotalMonths / 12) If NewMonth = 0 Then NewMonth = 12 NewYear = NewYear - 1 End If Select Case NewMonth Case 1, 3, 5, 7, 8, 10, 12 EOMonth = DateSerial(NewYear, NewMonth, 31) Case 4, 6, 9, 11 EOMonth = DateSerial(NewYear, NewMonth, 30) Case 2 If Int(NewYear / 4) = NewYear / 4 Then EOMonth = DateSerial(NewYear, NewMonth, 29) Else EOMonth = DateSerial(NewYear, NewMonth, 28) End If End Select End Function
Hi John,
Like the code you found, the following allows you to specify the number of months to add:
Public Function EndOfMonth(ByVal InputDate As Date, Optional AddMonths As Integer) As Date EndOfMonth = DateSerial(Year(InputDate), Month(InputDate) + AddMonths + 1, 0) End Function
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
I use something like this which picks the first day of the next month and subtracts 1 from it.
wDate = ’12/13/2009′ ‘ define date
wEOM = datevalue(month(wDate)+1 & “/01/” & year(wDate)) – 1
you have to trap for period 12 because the date function does not understand period 13 and assumes you’ve started using European date convention. So add:
if month(wDate) = 12 then
wEOM = datevalue(“12/31/” & year(wDate))
else
wEOM = datevalue(month(wDate)+1 & “/01/” & year(wDate)) – 1
end if
Other than the first reply, which seems unnecessarily complex and doesn’t calculate leap year correctly, all of the suggestions are only 1 or 2 lines long. The only other function is a single line long,
The advantage of a function is that it’s much easier to fix if you later discover a problem with it. If you do it inline, you’ll likely have lots of trouble even finding all of the places to fix.
Lessee, with only the Day function:
Public Function EOMonth(InputDate As Date) EOMonth = InputDate - day(InputDate) +45 EOMonth = EOMonth - day(EOMonth) end sub
–Scott.
Hi Bill,
Don’s request was for”a VBA solution for the Excel formula”. That’s what I and others have provided. I don’t know what it is that makes you’d think “you have to put code to call it” means it is at all complicated – it works pretty much the same as a formula. For example mine can be called as:
=EndOfMonth(A1)
or:
=EndOfMonth(A1, 3) – for the last day of the month three months hence.
That’s no more complex than any inbuilt Excel function.
Cheers,
Paul Edstein
[Fmr MS MVP - Word]
Hi,
This is probably very late, but I hope someone will find it useful. I guess I’m just being lazy, but I like Excel to do the work when possible. How about
Increment = 0 NewDate = Evaluate(“EOMONTH(” & MyDate & “,” & Increment & “)”)
and you can change the value of Increment as you like.
Donations from Plus members keep this site going. You can identify the people who support AskWoody by the Plus badge on their avatars.
AskWoody Plus members not only get access to all of the contents of this site -- including Susan Bradley's frequently updated Patch Watch listing -- they also receive weekly AskWoody Plus Newsletters (formerly Windows Secrets Newsletter) and AskWoody Plus Alerts, emails when there are important breaking developments.
Welcome to our unique respite from the madness.
It's easy to post questions about Windows 11, Windows 10, Win8.1, Win7, Surface, Office, or browse through our Forums. Post anonymously or register for greater privileges. Keep it civil, please: Decorous Lounge rules strictly enforced. Questions? Contact Customer Support.
Want to Advertise in the free newsletter? How about a gift subscription in honor of a birthday? Send an email to sb@askwoody.com to ask how.
Mastodon profile for DefConPatch
Mastodon profile for AskWoody
Home • About • FAQ • Posts & Privacy • Forums • My Account
Register • Free Newsletter • Plus Membership • Gift Certificates • MS-DEFCON Alerts
Copyright ©2004-2025 by AskWoody Tech LLC. All Rights Reserved.
Notifications