question about calculating last day of previous month

ces005

Board Regular
Joined
Mar 13, 2006
Messages
189
Hi,

If the current date is the last day of the current month, then I want to use that date, otherwise I want to use the last day of the previous month. How do you write this kind of logic? Thanks.

-Eileen
 

Excel Facts

Shade all formula cells
To shade all formula cells: Home, Find & Select, Formulas to select all formulas. Then apply a light fill color.

GorD

Well-known Member
Joined
Jan 17, 2004
Messages
1,446
How about

=IF(A1=EOMONTH(A1,0),A1,EOMONTH(A1,-1)) where your date is in A1 [=today()]

From memory EOmonth probably needs tha analysis tool pack installed.
 

ces005

Board Regular
Joined
Mar 13, 2006
Messages
189
Thanks a lot for your help.

I have the analysis tool pack installed already because of other date functions I am using.
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
49,401
Office Version
  1. 365
Platform
  1. Windows
ces005

Try this one:
=EOMONTH(A1+1,-1)
 

ces005

Board Regular
Joined
Mar 13, 2006
Messages
189

ADVERTISEMENT

formula worked

That worked really well. Thanks a lot.
 

ces005

Board Regular
Joined
Mar 13, 2006
Messages
189
If I am distributing this to a lot of people and don't want to ask everyone to install the analysis pack. Is there some other way to do this logic within a visual basic script? I think the pseudo-code looks something like this. EndDate is the last day of the previous month if the current date is not the last day of the month.

EndDate = Date
' determine the month and year of next month
if (current month is December)
set TempYear to (year + 1)
set TempMonth to Jan
else
set TempYear to current year
set TempMonth to (current month + 1)
end if

if (TempMonth/1/TempYear) - 1 = Date
' current date is last day in the month
' use that date
EndDate = Date
else
EndDate = (current month/1/current year - 1
end if



-Eileen
 

Peter_SSs

MrExcel MVP, Moderator
Joined
May 28, 2005
Messages
49,401
Office Version
  1. 365
Platform
  1. Windows

ADVERTISEMENT

ces005

If you do not want to rely on Analysis Toolpak, you still don't need VBA. Try this formula:
=DATE(YEAR(A1+1),MONTH(A1+1),1)-1
where A1 houses =TODAY()

This could be combined into a single formula:
=DATE(YEAR(TODAY()+1),MONTH(TODAY()+1),1)-1
though since TODAY() is a 'volatile' function, it is generally best to use it as few times as possible on a sheet.
 

ces005

Board Regular
Joined
Mar 13, 2006
Messages
189
question about applying formula within script

Hi Peter,

How would I apply the formula you gave within a visual basic script? Thanks.

-Eileen
 

Norie

Well-known Member
Joined
Apr 28, 2004
Messages
76,300
Office Version
  1. 365
Platform
  1. Windows
Eileen

Can you explain further how you want to use this in VBA?

Do you want to create a user defined function?
 

Forum statistics

Threads
1,136,260
Messages
5,674,695
Members
419,520
Latest member
Jennifer4Dillon

We've detected that you are using an adblocker.

We have a great community of people providing Excel help here, but the hosting costs are enormous. You can help keep this site running by allowing ads on MrExcel.com.
Allow Ads at MrExcel

Which adblocker are you using?

Disable AdBlock

Follow these easy steps to disable AdBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the icon in the browser’s toolbar.
2)Click on the "Pause on this site" option.
Go back

Disable AdBlock Plus

Follow these easy steps to disable AdBlock Plus

1)Click on the icon in the browser’s toolbar.
2)Click on the toggle to disable it for "mrexcel.com".
Go back

Disable uBlock Origin

Follow these easy steps to disable uBlock Origin

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back

Disable uBlock

Follow these easy steps to disable uBlock

1)Click on the icon in the browser’s toolbar.
2)Click on the "Power" button.
3)Click on the "Refresh" button.
Go back
Top