简体   繁体   中英

How to count all multiple cells in Excel

I am trying to count All the multiple colors assigned for one project in Excel column. I tried the below formula but its counting the only value which I requested. =COUNTIFS(C10:C17, G19, E10:E17, H19) .

**Project List      Colors**
Project a           Black 
Project b           Brown 
Project c           Red 
Project d           Orange 
Project a           Yellow 
Project b           Brown 
Project c           Red 
Project e           Black 
Project f           Green

In the above table, if selected a cell contain Project a, it should display 1 Black and 1 Yellow. If selected a cell contain project b, it should display 2 brown.

Edit: Colors are values its not a actual color or background color.

You can use the UDF

Function modifiedCountif(Rng1 As Range, Criteria1 As Range, Rng2 As Range)
Dim i As Long
Dim dict As Object
Set dict = CreateObject("Scripting.Dictionary")
Dim Result As String

Result = ""
For i = 1 To Rng1.Count
    If Rng1.Cells(i, 1) = Criteria1 Then
    On Error Resume Next
        dict.Add Rng2.Cells(i, 1).Value, Application.WorksheetFunction.CountIfs(Rng1, Criteria1, Rng2, Rng2.Cells(i, 1))
    End If
Next

Dim key As Variant
For Each key In dict.Keys
    Result = Result & dict(key) & " " & key & ", "
Next key

modifiedCountif = Left(Result, Len(Result) - 2)
dict.RemoveAll
End Function

Where Rng1 is the range of Project List

Criteria1 is the project you are searching for.

Rng2 is the Range for colors

I am not sure about your actual data ranges so following is my test setup.

  • Project List is in range C10:C18 .

  • Colors List is in range E10:E18 .

  • Input cell which holds a single value like Project b is I2 .

Then formula will be (to be committed by pressing CTRL + SHIFT + ENTER simultaneously while in edit mode. If applied correctly, Excel will wrap formula by {}).

=IFERROR(INDEX($E$10:$E$18,SMALL(IF($C$10:$C$18=$I$2,IF(MATCH($C$10:$C$18&$E$10:$E$18,$C$10:$C$18&$E$10:$E$18,0)=(ROW($E$10:$E$18)-9),ROW($E$10:$E$18),2097153),2097153),ROWS($A$1:A1))-9),"")

You will have to adjust the ranges to suit. Attention must be given to number 9 which is used because data starts at row 10. It should be always (n-1) assuming n is row where your data begins!

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