简体   繁体   中英

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

名册冲突-Joe Bloggs 4 @ 2014年5月21日的两个站点

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

What I've tried doing is summing/counting cells that are coloured with a few VBA methods:

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.

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

Appreciate any help/pointers, as it is, I'm stuck!

You won't be able to use SUMIFS and COUNTIFS for this. 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 . Then you can operate on the returned array, eg SUM it.

Example usage:

在此处输入图片说明

where the -- double minus converts Boolean TRUE/FALSE values (which are not summable) into 1's and 0's (which are).

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.

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 . That is the way to do it when you're dealing with array formulas.

Read more here: 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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