• Excel Book Excerpt

Excel Coerce an Array of Dates from 2 Dates

This page is an advertiser-supported excerpt of the book, Learn Excel 2007-2010 from MrExcel - 512 Excel Mysteries Solved. If you like this topic, please consider buying the entire e-book.

Coerce an Array of Dates from 2 Dates

Problem: I have a start date in A and an end date in B. I need to see how many days between those two dates fell on Friday the 13th.

Strategy: This formula came from the MrExcel.com message board. It is one of the coolest formulas that I have ever seen. This one formula will replace 108,000 formulas.

While this formula is figuring out the number of Friday the thirteenths, you can use this method any time you need to compare every date between two dates to something.

I will show you the formula first and then explain it in detail.

LE10000574.jpg

Figure 563 This one formula does 108,000 calculations.

The explanation here is about row 2. The same concept applies to row 3.

1. Remember that dates are really stored as the number of days elapsed since January 1, 1900. The date in A2 is really stored as 41680.

2. The date in B2 is really stored as 41691.

3. Concatenate those two numbers with a colon in between. You get 41680:41691. This is actually a valid Excel reference. It refers to all of the rows from 41680 to 41691.

4. Take the text from #3 and put it inside an INDIRECT(41680:41691) function. Since that is a valid reference, you now are referencing a range of all the rows from 41680 to 41691.

5. Use the ROW() function on the reference from step 4. You now have an array of row numbers: {41680; 41681; 41682; 41683; 41684; 41685; 41686; 41687; 41688; 41689; 41690; 41691}. Note that this array is relatively small. Down in row 3, there will be 18,000 numbers in the array. For the rest of the steps, stop thinking about those as numbers and start thinking about them as dates.

6. Take the WEEKDAY() of each number in the array from #5. You get {2; 3; 4; 5; 6; 7; 1; 2; 3; 4; 5; 6}. In this version of WEEKDAY, Monday is 1 and Friday is 5.

7. Test to see if the results of step 6 are equal to 5 which means Friday. You now have an array of True/False values: {False; False; False; True; False; False; False; False; False; False; True; False}.

8. Go back to that array from step 5 and put it in the DAY() function. This will return an array of only the day portion of the date: {10; 11; 12; 13; 14; 15; 16; 17; 18; 19; 20; 21}.

9. Take the array from 8 and test to see if it is 13. {False; False; False; True; False; False; False; False; False; False; False; False}.

10. Multiply the array from 7 by the array from 9. If both have a TRUE in the same position you will get a 1, otherwise a 0. You get: {0; 0; 0; 1; 0; 0; 0; 0; 0; 0; 0; 0}.

11. Sum all of the 1’s from step 10 using SUMPRODUCT. In this case, you get a 1 as the total. In the case of row 3, you get 86 occurrences of Friday the 13th in a span of 49 years.

It might help to picture the calculation happening in Excel.

LE10000577.jpg

Figure 564 All of these 11 steps happen inside of C2.

Picture the calculation for C3. instead of 12 items in each array, there are 18,000 items in each array. That one formula takes two date cells and coerces it into several arrays of 18,000 items each.

If you find this type of formula interesting, check out my other book, Excel Gurus Gone Wild.

<-Previous Topic Next Topic->

For more resources for Microsoft Excel:

privacy policy