Pushing Dates Into Last Month

by Allen Wyatt
(last updated August 1, 2018)

8

Kathy needs a formula that will change any date within the current month to the last day of the previous month. She also notes that it would be helpful to have a VBA macro that could do this to the dates in a range of selected cells.

There are actually several different formulaic approaches you could use to derive the desired date. These two are the shortest and easiest to use:

=A1-DAY(A1)
=EOMONTH(A1,-1)

The first one works because of a quirk that "day 0" of the current month is always considered to be the last day of the previous month. Thus, if the date in cell A1 is 9/25/14 you end up subtracting 25 (the DAY value) from that day, giving a date of 9/0/14, or 8/31/14.

The second formula works because of the inclusion of the second parameter for the EOMONTH function. This parameter is used to indicate how many months in the past (negative) or future (positive) the end of the month should be calculated.

In either case, you may need to format the cell containing the formula as a date. If you don't, what you see may be the underlying serial number used by Excel to calculate dates.

If you want a macro that will actually convert dates in a selected range into the last day of the previous month, the following will do the trick.

Sub LastDayPreviousMonth()
    Dim rCell As Range

    For Each rCell In Selection
        If IsDate(rCell) And Not rCell.HasFormula Then
            rCell.Value = WorksheetFunction.EoMonth(rCell.Value, -1)
        End If
    Next rCell
End Sub

The macro steps through each cell in the selected range and checks to see if it contains a date and is not a formula. (In other words, if the date in a cell is the result of a formula, the formula is not changed.) The EOMONTH function is then used, as previously described, to calculate the desired date.

It should be mentioned that the macro approach will also convert textual dates. For instance, if a cell contains, as text, the date "6/15/14," then the macro will change it to 5/30/14. Once the adjusted date is stuffed back into the cell, it will show as a date/time serial number, not as text.

Note:

If you would like to know how to use the macros described on this page (or on any other page on the ExcelTips sites), I've prepared a special page that includes helpful information. Click here to open that special page in a new browser tab.

ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13304) applies to Microsoft Excel 2007, 2010, and 2013.

Author Bio

Allen Wyatt

With more than 50 non-fiction books and numerous magazine articles to his credit, Allen Wyatt is an internationally recognized author. He is president of Sharon Parq Associates, a computer and publishing services company. ...

MORE FROM ALLEN

Creating a Table of Contents Involving Multiple Documents

Word can handle very large documents, but some people find it preferable to break large documents into multiple ...

Discover More

Copying a File

Making copies of files is a snap in Drive. Here are a couple of ways you can perform this common task.

Discover More

Rounded Corners on Cells

As you are formatting a worksheet, Excel allows you to easily add borders to cells. Adding rounded corners to cells is a ...

Discover More

Save Time and Supercharge Excel! Automate virtually any routine task and save yourself hours, days, maybe even weeks. Then, learn how to make Excel do things you thought were simply impossible! Mastering advanced Excel macros has never been easier. Check out Excel 2010 VBA and Macros today!

More ExcelTips (ribbon)

Finding the Previous Work Day

Simple math will tell you what the previous day is (just subtract 1 from today's date). What if you want to know the date ...

Discover More

Determining If a Year is a Leap Year

Need to figure out if a given year is a leap year? It's not as easy as you think! This tip provides a plethora of ways ...

Discover More

Elapsed Days as Years, Months and Days

Need to know how many days there are between two dates? It's easy to figure out—unless you need the figure in ...

Discover More
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

View most recent newsletter.

Comments

If you would like to add an image to your comment (not an avatar, but an image to help in making the point of your comment), include the characters [{fig}] in your comment text. You’ll be prompted to upload your image when you submit the comment. Maximum image size is 6Mpixels. Images larger than 600px wide or 1000px tall will be reduced. Up to three images may be included in a comment. All images are subject to review. Commenting privileges may be curtailed if inappropriate images are posted.

What is four more than 2?

2018-08-01 11:07:11

David Bonin

I still prefer a formula like: = DATE( YEAR( TODAY()), MONTH(TODAY()), 0 )

I willingly stipulate that it's not the shortest equation possible.

I write code in a corporate environment. While there are lots of clever ways to do things in Excel, I often find it is wiser not to be too clever. Why? Because someone else will have to maintain or modify my code when I am no longer here. My employer is not paying me to inflict pain on the people that follow me.

Going a step further in maintainability, note that I add spaces in my formulas to make them more readable. Yes, it makes my files slightly larger. It also makes them far more readable -- especially the complex ones. Sometimes, I'll even add line breaks to split up formulas into logical chunks.

Afterall,wedon'twriteEnglishsentenceswitnospacesbetweenthewords.It'sfartoohardtoread! So why do it with Excel?


2018-08-01 09:54:58

David Bauer

"The first one works because of a quirk that "day 0" of the current month is always considered to be the last day of the previous month. Thus, if the date in cell A1 is 9/25/14 you end up subtracting 25 (the DAY value) from that day, giving a date of 9/0/14, or 8/31/14."

Love your site, but I'm uncomfortable with the quote above. It's not a quirk. Dates are represented internally as the number of days from 1/1/1900 (or 1/1/1904, depending on options settings). When you subtract the day of the month, you are reducing the internal number to the number that represents the end of the previous month, not the 0 day of the starting month.


2014-11-13 00:38:35

Shreepad S M Gandhi

@ Michael Avidan

Can well understand the reason for your advice. What you may not know is a response was already sent seperately to
'etanswers@tips.net' when I received the Tips.Net Comments newsletter. But it was not credited. No issues at all as this happened with me for the first time. Have communicated it to Allen Wyatt too. My post below here is just a Copy-Paste of the response sent.

I am sorry if this post irritates you. By the way, I have also been a fan of your “Microsoft® Answers" - Wiki author & Forums Moderator.

Good day.


2014-11-10 09:34:59

CaroleJean

What would be some examples where this would be useful please?


2014-11-10 05:15:41

Michael (Micky) Avidan

@Gašper Kamenšek,
Some people say: "The length does not matter".
Well, my opinion is "The shorter the better" (as long Excel is concerned).
The formula:
=DATE(YEAR(TODAY()),MONTH(TODAY()),0)
is almost as twice(!) longer than:
=EOMONTH(TODAY(),-1)
Therefore it is no reason to use it.
*** Again - this is my opinion ***
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2015)
ISRAEL


2014-11-10 05:11:41

Michael (Micky) Avidan

@Shreepad S M Gandhi,
may I suggest you to read - ONCE MORE - the entire(!) article listed above !?
Michael (Micky) Avidan
“Microsoft® Answers" - Wiki author & Forums Moderator
“Microsoft®” MVP – Excel (2009-2015)
ISRAEL



2014-11-10 01:36:50

Gašper Kamenšek

There's actually another way to do this and it's a great trick with the DATE function, and great know how. More here
http://excelunplugged.com/2014/10/28/last-day-of-previous-month-excel/


2014-11-09 22:11:37

Shreepad S M Gandhi

No need for a macro at least to get the last day of the previous month of a certain date. Use the formula =EOMONTH(Start_Date, Month). You may understand it better with an example.
Suppose A1 is you reference date. And you wish to have in cell B1, the last date of previous month w.r.t the date in A1.

In cell B1, type the formula =EOMONTH(A1,-1)

Now you type any date in cell A1 and press Enter. You will get the last date of the previous month of date in A1. If you change -1 to 1 you will get the last date of next month of date in A1 and so on.


This Site

Got a version of Excel that uses the ribbon interface (Excel 2007 or later)? This site is for you! If you use an earlier version of Excel, visit our ExcelTips site focusing on the menu interface.

Newest Tips
Subscribe

FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."

(Your e-mail address is not shared with anyone, ever.)

View the most recent newsletter.