如何使用另一列中的文本匹配触发彩色单元格的计数

How to trigger count of colored cells using text match in another column

我想将 sheet 中的员工姓名与 sheet 中的员工姓名进行匹配,然后 运行 特定特定中所有黄色(填充)单元格的计数列。

我有一个 VBA 模块,它将 运行 突出显示的单元格的数量,而无需进行名称匹配,并且它工作得很好。现在我需要添加一个额外的指标,运行计算每个员工的所有突出显示的单元格。

数据信息:

Sheet One B2:B50 - list of employee last names.

Sheet Two D2:D1845 - column with employee last names. Note: This is a worksheet with 1845 line items of client data records and therefore the employee name could be listed numerous times in said column.

Sheet Two E2:E1845 - column with yellow-colored cells. Not all cells in the column are colored yellow. Which is why I need a count of how many are colored for each employee.

按颜色计数 VBA 有效:

Function CountByColor(InputRange As Range, ColorRange As Range) As Long
   
 Dim cl As Range, TmpCount As Long, ColorIndex As Integer
   
 Application.Volatile
   
 ColorIndex = ColorRange.Interior.ColorIndex
   
 TmpCount = 0
   
 On Error Resume Next
   
 For Each cl In InputRange.Cells
        If cl.Interior.ColorIndex = ColorIndex _
            Then TmpCount = TmpCount + 1
   
 Next cl
   
 CountByColor = TmpCount
    
End Function

我不太确定这是否是您想要实现的目标我还不能 post 图片。 此 Sub 在工作表 1 C2:C50 范围内为工作表 2 D2:D1845 中的每个员工插入一个计数器,旁边的单元格为黄色。

Sub Find_Matches()
    Dim CompareRange As Variant, x As Variant, y As Variant, CountA As Integer
    Set EmployeeRange = Worksheets("Sheet1").Range("B2:B50")
    Set CompareRange = Worksheets("Sheet2").Range("D2:D1845")
    For Each x In EmployeeRange
        For Each y In CompareRange
            If x = y And y.Offset(0, 1).Interior.ColorIndex = 6 Then CountA = CountA + 1
        Next y
        x.Offset(0, 1).Value = CountA
        CountA = 0
    Next x
End Sub

根据你向我解释的内容,你想在评论中使用这里的一行代码来做你想做的事:

Public Sub NameColorCount(NameToSearch As String, TargetCell As Range, _  
           SearchRange As Range, RangeToCountColor As Range, ColorRange As Range)

  If Not SearchRange.Find(NameToSearch) Is Nothing Then
    TargetCell.Value = CountByColor(RangeToCountColor, ColorRange)
  End If

End Sub

如果您想在单元格中执行此操作,您可以将 CountByColor 用作 UDF 并使用以下公式:

=IF(COUNTIF(D:D, B1)>0,CountByColor(E:E, B1),"")

假设您的 ColorRange 是 'B' 单元格,否则修改