Discussion Locked

Date Formula in Excel

0 helpful
10 replies
Closed to new repliesThis discussion is closed. It stays readable, but it takes no new replies.
I use spreadsheets for manpower analysis. With changes in WTR I need a formulae that will take a start date from any given year and recalculate it so that it I get a year end date relevant to the current year, which other formulaes rely on to display how many weeks are left of the current holiday year. It is the initial formulae I am having problems with. Help please

10 replies

by dklippert

Have you tried Weeknum? This gives you the number of the week for a particular date. You can, of course subtract that from 52. You need to have the Analysis pak installed to use this function.
You might also look at YearFrac to find out the fraction of a year represented by two dates. This function can use 30/360 as a basis if you need it.

by Former member

This doesn't help as the start dates vary over a four year period. In more detail, I need a formula that returns a specific value if someone started beofre todays date in '98 and another if they started after. I now believe that "IF" formulae willdo the job, without having to install the addins. this would involve my Companies Computer department visiting 35 sites!

I do believe this will help me on othr projects though. Thanks

by kewharp

Recently someone (sorry! I don't remember who or what question!) answered a question and referenced the following website that may be of help to you. It has some great info on it. The specific page where I have sent you has a function that may be what you need. Otherwise, the bottom of the page lists many other options using date/time.

http://www.cpearson.com/excel/datedif.htm

by bill_h

(Pursuant to our e-mail discussions...)

OK...I think the following code will come reasonably close to accomplishing what you want.

Private Sub Worksheet_Change(ByVal Target As Range)
If Target.Column = 9 Then
If IsDate(Target.Value) Then
Sheet1.Cells(Target.Row, 17) = Calculate_YearEnd(Target.Value, Sheet1.Range("$B$1").Value)
End If
End If
End Sub

Private Function Calculate_YearEnd(ByVal dtStart As Date, ByVal dtCurrDate As Date) As String Dim dtTemp As Date

dtTemp = DateAdd("d", -1, Month(dtStart) & "/" & Day(dtStart) & "/" & Year(dtCurrDate))
If dtTemp < dtCurrDate Then
Calculate_YearEnd = DateAdd("yyyy", 1, dtTemp)
Else
Calculate_YearEnd = dtTemp
End If
End Function

The first subroutine ("Worksheet_Change") is triggered when a column in the worksheet changes; if the column number changed is 9 (relating to column "I", the "Start Date"), and the contents of the column are a valid date, then the "Calculate_YearEnd" function is called.

Using the dtStart and dtCurrDate parameters passed to it, the "Calculate_YearEnd" sets the dtTemp variable to the month and day of the "Start Date" and the year of the current date ($B$1), andsubtracts one day. Then (and I added this without knowing if it was applicable), if the resulting date is LESS than the current date, then the date is bumped to the next year. The "Calculate_YearEnd" returns the calculated date value.

The "Worksheet_Change" routine places the returned date into column 17 ("Year End Date") of the row number indicated by Target.Row.

You can place this code into the spreadsheet using the Visual Basic editor, and test it. If you are not sure how to do this, drop me a line.

by donq

Please rephrase your question????

[CurrentDate] - [StartDate] = ?

I assume you are looking for the number of weeks ("ww") until something happens, or just happened?

by Former member

This question was closed by the author

This discussion is closed to new replies.