简体   繁体   English

在 EXCEL/GOOGLE SHEETS 中计算具有多个条件和范围的多个值

[英]Count multiple values with multiple conditions and ranges in EXCEL/GOOGLE SHEETS

In my company employees should get a day off if:在我公司,如果出现以下情况,员工应该休息一天:
1. they work on a friday nightshift(NS) 1. 他们上周五夜班(NS)
2. they work on a thursday NS and a saturday NS 2. 他们在星期四和星期六工作
3. they work friday morning and saturday NS. 3. 他们在星期五早上和星期六国民服役。

I need to count how many dayoffs every employee should get我需要计算每个员工应该得到多少休假

MORNING                                  NIGHT                   WEEKDAY |DAY
PLACE1|PLACE2|PLACE3 PLACE4|PLACE5|PLACE6|
DAV FL RI RI 1 10
DAV RI FL DAV 2 11
FL RI DAV FL 3 12
DAV RI FL DAV 4 13
FL RI DAV RI FL 5 14
DAV 6 15
FL DAV 7 16
DAV FL RI RI 1 17
DAV RI FL DAV 2 18
FL RI DAV FL 3 19
DAV RI FL DAV 4 20
FL RI DAV RI 5 21
FL DAV 6 22
RI FL 7 23

In this example FL gets 2 days off在这个例子中 FL 休假 2 天
RI gets 1 day off RI 休假 1 天
DAV gets 2 days off DAV 休息 2 天

Counting the nightshifts of friday could be achieved with COUNTIFS.使用 COUNTIFS 可以计算周五的夜班。

=COUNTIFS(D3:D34,"DAV",G3:G34,"6")+COUNTIFS(E3:E34,"DAV",G3:G34,"6")+COUNTIFS(F3:F34,"DAV",G3:G34,"6")

but things get complicated when I have to use the name condition on 6 different columns, each with a different weekday condition, ie employee on T+SAT or F+SAT, Is there any way to achieve this using only built-in formula w/o scripts?但是当我必须在 6 个不同的列上使用名称条件时事情变得复杂,每个列都有不同的工作日条件,即 T+SAT 或 F+SAT 上的员工,有没有办法只使用内置公式 w/ o 脚本?

You can try the following formulas:您可以尝试以下公式:

Friday nights simply:周五晚上简单:

=SUMPRODUCT((L2=$D$3:$F$16)*(6=$G$3:$G$16))

Thursday nights + Saturday nights more complicated:周四晚上 + 周六晚上更复杂:

=MIN(SUMPRODUCT((L2=$D$3:$F$16)*(5=$G$3:$G$16)),SUMPRODUCT((L2=$D$3:$F$16)*(7=$G$3:$G$16)))

The principle of calculation is that Thursday nights worked are counted separately and Saturday nights are counted separately then from the two numbers are selected smallest.计算的原则是星期四晚上工作分开计算,星期六晚上分开计算,然后从两个数字中选择最小的。 This in turn means that a person can work one week on a Thursday and after two weeks on a Saturday and get a day off.这反过来意味着一个人可以在周四工作一周,两周后在周六工作,然后休息一天。 See if it's right.看看对不对。

Friday mornings + Saturday nights by analogy:周五早上 + 周六晚上类比:

=MIN(SUMPRODUCT((L2=$A$3:$C$16)*(6=$G$3:$G$16)),SUMPRODUCT((L2=$D$3:$F$16)*(7=$G$3:$G$16)))

在此处输入图片说明

I transferred your sample data to a worksheet so that your morning shift data are in columns A:C and the night shift in columns E:G.我将您的示例数据传输到工作表,以便您的早班数据在 A:C 列中,而夜班数据在 E:G 列中。 The critical weekday column is column H. I presumed that your data have 1 = Sunday.关键工作日列是 H 列。我假设您的数据有 1 = 周日。

I created two named ranges, "AM" and "PM", to cover the columns for morning and night shifts.我创建了两个命名范围“AM”和“PM”,以涵盖早班和夜班的列。 It's important that the ranges start from row 1. Note : It's also of critical importance that my formula not be written in a row above row 3 in the worksheet.范围从第 1 行开始很重要。注意:我的公式不要写在工作表中第 3 行上方的一行中也很重要。

I added columns to your layout on the right, one for each employee, and wrote the employees' name in row 2 of each column.我在右侧的布局中添加了列,每个员工一列,并在每列的第 2 行写下员工的姓名。 In my worksheet these columns are at K:M, and the formula goes to K3.在我的工作表中,这些列位于 K:M,公式变为 K3。 From there it is copied to all other cells in my three columns.从那里它被复制到我的三列中的所有其他单元格。

=IF($H3=6,IF(COUNTIF(INDEX(PM,ROW(),0),K$2),1,""),IF($H3=7,IF(COUNTIF(INDEX(PM,ROW(),0),K$2),IF(OR(COUNTIF(INDEX(AM,ROW()-1,0),K$2),COUNTIF(INDEX(PM,ROW()-2,0),K$2)),1,""),""),""))

The three columns I added show a 1 for each earned off-day.我添加的三列显示每个休息日的收入为 1。

The formula discards all days which aren't either Friday or Saturday.该公式丢弃所有不是星期五或星期六的日子。 On Fridays it awards a "1" if the name appears in the PM shift.在星期五,如果名称出现在 PM 班次中,它会奖励“1”。 For Saturday PM work and off-day is awarded if there was also work on either Friday AM or Thursday PM in the same week.对于周六下午的工作,如果在同一周的周五上午或周四下午也有工作,则奖励休息日。 From this rule follows that you can't start your months on the first day if that is a Saturday.根据此规则,如果那是星期六,则您不能从第一天开始您的月份。 The preceding Thursday must be included in the month's data.前一个星期四必须包含在该月的数据中。 If it's not no error will occur (unless the formula is in row 2) but the result will be wrong due to the referenced row containing no valid data.如果不是,则不会发生错误(除非公式在第 2 行),但由于引用的行不包含有效数据,结果将是错误的。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM