Click here to Skip to main content
15,884,739 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
Hi,
I have following dates in a excel sheet
5/27/2015
5/26/2015
4/25/2015
2/24/2015
5/23/2015
5/22/2014
3/21/2014
2/20/2014
6/19/2014
7/18/2013


I need to find the end of months for the above dates in another sheet. So, my output should be
5/31/2015
4/30/2015
2/28/2015
6/30/2014
5/31/2014
3/31/2014
2/28/2014
7/31/2013


Any VBA code or excel formula would help. and also thinking that first table has another column with interest and including the Outstanding Interest accured for a month in the second table will help tremendously.

Thank you for your valuable solutions.
Posted
Updated 18-Jun-15 8:49am
v3

try with EOMONTH function for example you can calculate month end of A cell value as
=EOMONTH(A1,0)
 
Share this answer
 
Comments
Andy Lanng 18-Jun-15 11:15am    
DOH! you beat me to it :Þ
Maciej Los 18-Jun-15 14:49pm    
5ed!
The algorithm is well known and was advised in many places. You can use DATEADD:
http://www.techonthenet.com/excel/formulas/dateadd.php[^].

The idea is:

Let's say you have some date D. Add one month and than subtract one day. You will arrive at the last day of the month of D. This is the main part of it.

To get to next month, set the date D to the first day of its month and repeat the step explained above. Repeat it in loop checking if your current date is still inside required time frame.

That's all.

—SA
 
Share this answer
 
Comments
Sundar9057 18-Jun-15 11:22am    
Hello, I have included the result needed for the mentioned problem. Can anyone help me required steps. I'm unable to follow you :(
Sergey Alexandrovich Kryukov 18-Jun-15 11:24am    
What have you tried so far?
Which part of my explanation is unclear?
—SA
Sundar9057 18-Jun-15 11:34am    
I need to have a unique combination of month and year for the first range sorted in descending order and then find the last day for each month and year. I'm unaware how to achieve this in excel with formula alone. any vba code to compute this will also help. thank you
Sergey Alexandrovich Kryukov 18-Jun-15 11:58am    
I just explained how to achieve it; did you try it out?
—SA
Sundar9057 18-Jun-15 12:00pm    
yes. but the dates are not continuous. there may be missing months for any given year. I've gone blank
In addition to the solution 1 by DamithSL[^] and solution 2 Sergey Alexandrovich Kryukov[^] here is an implementation of EOMONTH formula (function):
VB
Function GetEofMonth(ByVal d As Date) As Date

    GetEofMonth = DateSerial(Year(d), Month(d) + 1, -1)

End Function


Usage:
VB
Sub Whatever()
Dim wsh As Worksheet
Dim i As Integer

Set wsh = ThisWorkbook.Worksheets("Sheet1")
i = 2
Do While wsh.Range("A" & i)<>""
    wsh.Range("B" & i)= GetEofMonth(CDate(wsh.Range("A" & i))) 'returns 5/30/2015
    i = i+1
Loop
Set wsh = Nothing
End Sub
 
Share this answer
 
Comments
Sergey Alexandrovich Kryukov 18-Jun-15 15:06pm    
Sure, a 5.
—SA
Maciej Los 18-Jun-15 15:38pm    
Thank you, Sergey.

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900