Pushing Dates Into Last Month

Written by Allen Wyatt (last updated May 8, 2021)
This tip applies to Excel 2007, 2010, 2013, 2016, 2019, and Excel in Microsoft 365


1

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 "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/21 you end up subtracting 25 (the DAY value) from that day, giving a date of 9/0/21, or 8/31/21.

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/21," then the macro will change it to 5/30/21. 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, 2013, 2016, 2019, and Excel in Microsoft 365.

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

TOC Heading Numbers Always Show in Bold

Linda's got a document that includes a table of contents that is based on headings in the document. When the headings ...

Discover More

Combining Cell Contents

Excel allows you to easily combine text together. Interestingly, it provides two ways you can perform such combinations. ...

Discover More

Calculating the Median Age of a Group of People

Suppose you have a worksheet that contains a list of ages and then a count of people who correspond with those ages. You ...

Discover More

Professional Development Guidance! Four world-class developers offer start-to-finish guidance for building powerful, robust, and secure applications with Excel. The authors show how to consistently make the right design decisions and make the most of Excel's powerful features. Check out Professional Excel Development today!

More ExcelTips (ribbon)

Converting UNIX Date/Time Stamps

If you import information generated on a UNIX system, you may need to figure out how to change the date/time stamps to ...

Discover More

ISO Week Numbers in Excel

Work in an industry that uses ISO standards when it comes to working with dates? You'll love the formula in this tip ...

Discover More

Calculating Fractions of Years

When working with dates and the relationship between dates, Excel provides a variety of worksheet functions that may ...

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}] (all 7 characters, in the sequence shown) 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 two less than 3?

2021-05-08 11:38:26

Rick Rothstein

You have these two lines of code in your LastDayPreviousMonth macro...

For Each rCell In Selection
If IsDate(rCell) And Not rCell.HasFormula Then

Rather than checking inside the loop to see if each cell does not having a formula in it, eliminate that part of the check and let Excel filter the cells you look at instead. To do that change the above two lines of code to these...

For Each rCell In Selection.SpecialCells(xlConstants)
If IsDate(rCell) Then


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.