Here is a task in Excel. In column A, there is a list of trading days. We want to pick out the last day each week in the list and put those days in Column B. For a simple example, in the following list. They are trading days in January 2007.

You have a handful of options depending on how you want to process things and depending on what you mean by "end of week".

If "end of week" means Friday like the examples above, you can get this information from one of two formulas:

=TEXT(A1, "ddd") will return the three character shorthand for the day (e.g. "Fri")
=MOD(A1, 7) will return the remainder when dividing by 7 (e.g. 6, for Friday)

Option 1 is easier to understand; Option 2 is generally faster.

Either of those formulas could be copied down column B and used to filter for the Friday value. If you just want the matching results, you can run a quick VBA through column A using either formula above to print results. If you need updating formulas in column B which only return matching results, it can be done but gets a little fancy. That formula would depend on ranges and be an array formula, but look something like this using the MOD approach above.

If you can live with helper columns, this can be done the way you want. The date is stored as a number. When this number is divided by 7 and floored, you now have a "week number". The idea then is to find the greatest dates present for each week present.

See the attached file for a solution. This would probably be done much cleaner with VBA if that is acceptable. If not, I think you are stuck with some helper columns. At least, that's the best I can do. You can always copy/paste values and delete the helper columns if that's OK.

Note that I have named the input dates "dates" and my helper column with week numbers "weeks". This cleans up the formulas a bit.

Hopefully you can see how to modify this for your dates. Note this assumes that "offending" dates will not be present in the input. If they are there, then this really cannot be done. Last-day-of-week-sample.xlsx

0

ResourcefulDBAuthor Commented:

@byronwall

Thank you. there is no problem of using helper column. I like your idea of getting the "week number" in column C and D. I wonder if Column D can handle the missing week, such as the following trading dates.

Once we have the week number, then we can get the Friday of that week. Then arrange the original trading date in the ascending order and use vlookup to look for the date closest to the Friday of that week. It should be handle something like the following sequence.

See that July 4th was a Friday, but was not a trading date. But if we use vlookup to look for all the dates in this list that is approximate to 7/4/2008, it will give us 7/3/2008 and it is the last trading date of that week. Wanna go for it?

It should already work as expected without having to do additional VLOOKUPs. When I paste in the July dates you give, the spreadsheet outputs:

7/3/2008
7/11/2008
7/18/2008
7/25/2008
7/31/2008

in column B. I believe this is what you are expecting for output? For the September dates, it spits out:

9/7/2001
9/10/2001
9/21/2001
9/28/2001

which also appears correct.

The main thing now is expanding the formulas so that they cover the full range of inputs that you are expecting. If you paste in the full range of dates for column A, you can then:
* fill columns B, C, and D down to the end of the data (B and D technically only need to go as far as the number of outputs, but you may as well drag them all the way down)
* update the named ranges "dates" and "weeks" to end at the correct row.

If you can make those changes, you are good to go. If not, tell me how many inputs you have, and I will update it. Or, alternatively, I can update the spreadsheet for 1000 or so inputs, and you can batch them in.

Expanding on ByronWall's suggestion, using CEILING or FLOOR can round a date up or down to the last day of the week.

For example:
=CEILING(TODAY(),7) gives 1 Nov 2014.

=FLOOR(TODAY(),7) gives 25 Oct 2014

If you want to adjust to the Friday then just put -1 on the end.

The syntax for CEILING and FLOOR are:

=CEILING(Number,Factor)

Number - any number
Factor - the multiple to which you want to round.

With dates being stored as numbers and day 1 of the Excel calendar being 1 Jan 1900 which was a Sunday, every Saturday thereafter is a multiple of 7 so rounding up or down to a factor of 7 gives the Saturday date.

Combining this with a MATCH could then check for holidays and deducting a further day to return the Thursday if required.

=CEILING(TODAY(),7)-IF(ISERROR(MATCH(TODAY(),Holidays,0)),1,2) Holidays = list of holiday dates

Yes, I tested out your formula and it did give out nice results.

I notice that you use array formula in Column B, and find out the max of of all dates for the given week. that is very clever. it can be better than vlookup in this way

Thank you.

0

Featured Post

Can Concerto Cloud Services help you focus on evolving your application offerings, while delivering the best cloud experience to your customers? From DevOps to revenue models and customer support, the answer is yes!