Summarizing data with dates can be difficult. For example, if you have a list of invoices with various dates, how do you summarize that for reporting? If you want to report on weekly figures, you could use a helper column and drop in a
=WEEKNUM(cell) formula to get an integer of the week. To report on monthly figures, you could use
=MONTH(cell) to get the integer of the number of the month (1 for January).
But when looking at reports, nobody wants to see week numbers of the number of the month—they want to see something relatable. We're going to look at three solutions to create fields to report on: 1. Week Beginning: for each invoice date, this gives us the Sunday date of that week. 2. Week Ending: for each invoice date, this gives us the Saturday date of that week. 3. Week Period: this gives us the Sun-Sat dates (03/31/19 - 04/06/19)
We'll then turn the Week Period into an add-in so that we can reuse it with ease.
Here's the data we'll be working with. Copy and paste it into your spreadsheet:
Let's drop in three helper columns to the right of our data:
F2, enter the formula
G2, enter the formula
Which gives us (make sure these cells are formatted as dates):
And now in cell
H2 we will combine these formulas to get the Sun-Sat dates. They have to be formatted as text, otherwise the underlying integer date values will just be added together and we would end up with some bizarre future date.
=TEXT(A2-WEEKDAY(A2)+1, "mm/dd/yyyy")&" - "&TEXT(A2-WEEKDAY(A2)+7, "mm/dd/yyyy")
Drag the formula's down and you end up with three helper columns to help you report on the data.
Creating the Add-in
The formula's above can be difficult to remember. With a user-defined function (UDF) we can re-use these formula's in any workbook we want just like we would with any built-in Excel formulas.
- Open a separate workbook and right click on the worksheet tab: select
In the menu, click Insert > Module.
Copy and paste the following into the white space:
Function WEEKBEGINNING(rng As Range) WEEKBEGINNING = (rng - Weekday(rng) + 1) End Function Function WEEKENDING(rng As Range) WEEKENDING = (rng - Weekday(rng) + 7) End Function Function BILLINGPERIOD(rng As Range) BILLINGPERIOD = Format((rng - Weekday(rng) + 1), "mm/dd/yy") & " - " & Format((rng - Weekday(rng) + 7), "mm/dd/yy") End Function