by Allen Wyatt
(last updated August 1, 2018)
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:
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.
ExcelTips is your source for cost-effective Microsoft Excel training. This tip (13304) applies to Microsoft Excel 2007, 2010, and 2013.
Create Custom Apps with VBA! Discover how to extend the capabilities of Office 2013 (Word, Excel, PowerPoint, Outlook, and Access) with VBA programming, using it for writing macros, automating Office applications, and creating custom applications. Check out Mastering VBA for Office 2013 today!
Need to copy a range of dates and update them to a different year? Here are a number of ways to accomplish this task with ...Discover More
Need a way to enter dates from every second Tuesday (or some other regular interval)? Excel makes it easy, providing ...Discover More
Excel includes a large number of functions that can be used in evaluating the data in a worksheet. In this tip you learn ...Discover More
FREE SERVICE: Get tips like this every week in ExcelTips, a free productivity newsletter. Enter your address and click "Subscribe."
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.