Добавил:
Опубликованный материал нарушает ваши авторские права? Сообщите нам.
Вуз: Предмет: Файл:
Power excel 2016 with mrexcel Master Pivot Tables, Subtotals, Charts, VLOOKUP, IF, Data Analysis in Excel 2010–2013 (Bill Jelen) (z-lib.org).pdf
Скачиваний:
4
Добавлен:
14.08.2022
Размер:
49.75 Mб
Скачать

216

POWER EXCEL WITH MR EXCEL

 

 

Figure 535 You can set the weekend.

Column E in Figure 534 calculates a Monday-Saturday workweek with =NETWORKDAYS. INTL(C3,B3,11,$I$3:$I$10).

Be aware that Excel is counting both the beginning and ending date. From Monday 2/17 to Monday 2/24, the NETWORKDAYS is calculating six days. That may not be the best answer at 5PM on Monday 2/17.

Additional Details: Enter the holidays on another worksheet and name the range something like HOLI-

DAYS. You don’t have to worry about inadvertently deleting a project and wiping out one of the holidays out to the right.

An alternate strategy to protect the holidays out in I is to select cells J3:J10. Enter =1 and then press Ctrl+Shift+Enter. This will create a lame array formula in column J. If anyone tries to delete a row from 3 to 10, Excel will refuse with the somewhat cryptic, “You Can Not Change Part Of An Array” message.

If you have a starting date and want to go out 15 work days from the starting date, take a look at the

WORKDAY and WORKDAY.INTL functions.

CALCULATE WORK DAYS FOR A FARMERS MARKET

Problem: Those new settings in NETWORKDAYS.INTL don’t help me. Our business is only open Mon- day, Wednesday, Friday, and Saturday.

Strategy: There is an alternate way to specify the third argument. Instead of specifying 1-7 or 11-17, you can specify a 7-digit text string comprised of 1’s and 0’s. The first character in the text is Monday. The seventh character is Sunday. Remember the name of this argument is [weekend]. Thus, a 1 indicates that the business is closed and a 0 indicates that the business is open.

Figure 536 Use the “0010001” argument for Barber Shops.

CALCULATE AGE IN YEARS, MONTHS, DAYS

Problem: I work in Human Resources. On our employee census, I need to calculate age in years and months.

Strategy: Use the super-secret DATEDIF function. Microsoft documented this function in Excel 2000 and never spoke of it since. Yet, it has been in Excel since the mid-nineties.

Use =DATEDIF(Earlier Date, Later Date, Return_Code).

PART 2: CALCULATING WITH EXCEL

217

 

 

The return codes are not entirely intuitive. They are shown here.

Figure 537 The third argument of DATEDIF.

From that list, the Y and YM codes would solve the question at the top of this topic. The following shows Years, Months, and Days.

2

Figure 538 DATEDIF calculates years, months, and days.

The less popular return codes are M for a complete count of full months, D for a complete count of days, and YD for the number of days in excess of full years.

Figure 539 Less popular return codes are M, D, and YD.

I’ve seen people get fancy with DATEDIF, using formulas such as these.

Figure 540 Concatenating multiple DATEDIF functions.

The formula in K is

=DATEDIF(B4,C4,”Y”)&” years, “&DATEDIF(B4,C4,”YM”)&” Months, “&DATEDIF(B4,C4,”MD”)&” days.”

The formula in L is

=DATEDIF(B4,C4,”Y”)&” years, “&DATEDIF(B4,C4,”YD”)&” days.”

218

POWER EXCEL WITH MR EXCEL

 

 

The formula in M is

=DATEDIF(B4,C4,”Y”)&”.”&DATEDIF(B4,C4,”YM”)

Gotcha: Here is the reason why Microsoft stopped documenting DATEDIF. When you calculate the DATE- DIF between January 31 and March 1, you get 1 month and negative two days. It was probably easier to stop documenting DATEDIF than to explain why this happens.

Figure 541 There are 29 days between 1/31 and 3/1. Not quite a month.

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 be- tween two dates to something. I will show you the formula first and then explain it in detail.

Figure 542 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 42045.

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

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

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

5. Use the ROW() function on the reference from step 4. You now have an array of row numbers: {42045; 42046; 42047; 42048; 42049; 42050; 42051; 42052; 42053; 42054; 42055; 42056}. 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 {3; 4; 5; 6; 7; 1; 2; 3; 4; 5; 6; 7}. In this version of WEEKDAY, Sunday is 1 and Friday is 6.

7. Test to see if the results of step 6 are equal to 6 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}.

PART 2: CALCULATING WITH EXCEL

219

 

 

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 84 occurrences of Friday the 13th in a span of 49 years.

It might help to picture the calculation happening in Excel.

2

Figure 543 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.

USE REAL TIMES

Problem: How does Excel deal with time?

Strategy: In Excel, 1 day is represented by the number 1. That means that time is stored as a decimal portion of a day. Go to any cell. Enter 0.5. Format the cell as time and you will get 12 noon, because half of the day has elapsed at noon.

I can name a few other times right off the top of my head. 6AM is stored as 0.25. 9PM is 0.875. 3AM is

0.125. Beyond that, it gets a tougher. What fraction of the day has elapsed at 3:42PM. That is a little tougher to calculate in your head.

Figure 544 Format the numbers in row 1 as time and you get row 3.

In the next figure, I used =RAND() in a bunch of cells and formatted those as time.

Figure 545 Random decimals correspond to various times of the day.