[英]Counting cells based on their colour and other criteria
I'm working on some rostering functions in Excel, with the basis being that we have a limited number of people that will need to be rostered-on for work, covering shifts at multiple sites 我正在使用Excel中的一些排班功能,其依据是我们的工作人员数量有限,涉及多个站点的班次
When planning the rosters, we use a simple set of coloured cells to fill-in for when people are on dayshift, nightshift, not-rostered, annual leave, etc. Given that there are multiple sites we plan for I need to have a quick formula that checks whether that individual has been rostered-on for simultaneous shifts at multiple sites (obviously not possible), so we can easily identify when there is a conflict in the planning stage. 在规划花名册时,我们使用一组简单的彩色单元格来填充人们白天上班,夜班,不安排名册,年假等情况。鉴于我们计划有多个地点,我需要快速公式,用于检查该个人是否已在多个地点同时轮班(显然不可能),因此我们可以轻松地确定计划阶段何时存在冲突。 The formula needs to return a TRUE, or value (eg. count >1 means more than one assignment for that person) so that I can use either conditional formatting or VBA to highlight a cell/the row and draw attention to the conflict
公式需要返回TRUE或值(例如,count> 1表示该人的一项分配),以便我可以使用条件格式或VBA突出显示单元格/行并引起对冲突的注意
What I've tried doing is summing/counting cells that are coloured with a few VBA methods: 我尝试做的是对一些VBA方法着色的单元格进行求和/计数:
Function ISFILLED(MyCell As Range)
If MyCell.Interior.colorIndex > 0 Then
Result = True
Else
Result = False
End If
ISFILLED = Result
End Function
and/or: 和/或:
Function ColorFunction(rColor As Range, rRange As Range, Optional SUM As Boolean)
Dim rCell As Range
Dim lCol As Long
Dim vResult
lCol = rColor.Interior.ColorIndex
If SUM = True Then
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = WorksheetFunction.SUM(rCell, vResult)
End If
Next rCell
Else
For Each rCell In rRange
If rCell.Interior.ColorIndex = lCol Then
vResult = 1 + vResult
End If
Next rCell
End If
ColorFunction = vResult
End Function
These both work fine on their own but I'm unable to combine this with a SUMIFS/COUNTIFS style function to only count the number of cells that are coloured when that individual's name appears against that assignment. 两者都可以正常工作,但是我无法将其与SUMIFS / COUNTIFS样式功能结合使用,以仅在该人的姓名出现针对该分配的情况下才计算彩色单元格的数量。
If you have a look in the sample image from the roster, you can see that Joe Bloggs 4 has been assigned a shift at both Site 1 and Site 2 on the 21/05/2014. 如果您从名单中查看示例图片,则可以看到2014年5月21日在站点1和站点2上为Joe Bloggs 4分配了班次。 What I'm after is essentially to count number of coloured cells on row, if the individuals name matching the criteria is against those cells .
我所追求的基本上是,如果符合条件的个人名字与那些单元格相对,则对行中彩色单元格的数量进行计数 。 For this example if would be
对于此示例,如果
=COUNTIFS(C8:AQ8, "Joe Bloggs 4", C12:AQ12, *Cell is Coloured*)
Colour of the cell doesn't matter (hence the first function ISFILLED is better as i don't need a reference cell for the fill), as it's just a sense-check. 单元格的颜色无关紧要(因此,第一个功能为ISFILLED更好,因为我不需要填充参考单元格),因为它只是一种感觉检查。
Appreciate any help/pointers, as it is, I'm stuck! 感谢所有帮助/指针,因为它卡住了!
You won't be able to use SUMIFS
and COUNTIFS
for this. 您将无法为此使用
SUMIFS
和COUNTIFS
。 You need something along these lines: 您需要遵循以下原则:
Function IsFilledArr(rng As Range) As Variant
Dim i As Long, j As Long
Dim v As Variant
ReDim v(1 To rng.Rows.Count, 1 To rng.Columns.Count)
For i = 1 To rng.Rows.Count
For j = 1 To rng.Columns.Count
v(i, j) = rng.Cells(i, j).Interior.ColorIndex > 0
Next j
Next i
IsFilledArr = v
End Function
This UDF can be called as an array formula, ie select a range of cells, type the formula in the top-left cell (while the whole range is still selected), and then press Ctrl - Shift - Enter . 此UDF可以称为数组公式,即选择一个单元格范围,在左上角的单元格中键入公式(同时仍选择整个范围),然后按Ctrl - Shift - Enter 。 Then you can operate on the returned array, eg
SUM
it. 然后,您可以对返回的数组进行操作,例如对它
SUM
。
Example usage: 用法示例:
where the --
double minus converts Boolean TRUE/FALSE values (which are not summable) into 1's and 0's (which are). 其中
--
双负布尔值TRUE / FALSE的值(未累加)转换成1和0(这是)。
I'll let you adapt this to suit your particular requirements. 我将让您对其进行调整以适合您的特定要求。
EDIT: You want more: 编辑:您想要更多:
calculate the number of filled cells in column C, where there was an adjacent filled cell in column B - in this case C4 would be the only cell that met that criteria.
计算C列中的填充单元数,其中B列中有一个相邻的填充单元-在这种情况下,C4是唯一满足该条件的单元。
This formula will do the trick (entered as an array formula): 这个公式可以解决问题(作为数组公式输入):
=SUM(IF(IsFilledArr(B2:B7)+IsFilledArr(C2:C7)>1,1))
Note the use of SUM(IF(...,1))
to mimic COUNT
. 注意使用
SUM(IF(...,1))
模仿COUNT
。 That is the way to do it when you're dealing with array formulas. 处理数组公式时,这就是这样做的方法。
Read more here: http://www.cpearson.com/excel/ArrayFormulas.aspx 在此处阅读更多信息: http : //www.cpearson.com/excel/ArrayFormulas.aspx
You asked for help/pointers; 您要求帮助/指针; this should hopefully be more than enough to get you unstuck.
希望这足以使您摆脱困境。 You can adapt this approach to meet whatever you exact needs are.
您可以采用这种方法来满足您的确切需求。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.