在excel中查找重复次数最多的值,但仅限于特定颜色的单元格

在excel中查找重复次数最多的值,但仅限于特定颜色的单元格,excel,Excel,嗨 我知道这个公式 =MODE(!B:B) 提供列B中重复次数最多的值,但我只想计算彩色单元格。这可能吗 多谢各位 编辑: 这就是我的模块的外观: Function GetInfo(TopObj As Variant, PropertySpec As Variant) As Variant Dim PropArr As Variant ' array returned by Split of object tree Dim ItemSpec As Variant ' item in coll

我知道这个公式

=MODE(!B:B)
提供列B中重复次数最多的值,但我只想计算彩色单元格。这可能吗

多谢各位

编辑:

这就是我的模块的外观:

Function GetInfo(TopObj As Variant, PropertySpec As Variant) As Variant

Dim PropArr As Variant ' array returned by Split of object tree
Dim ItemSpec As Variant ' item in collection
Dim Obj As Object ' generic Object to hold
                  'the top-level object (ws,wb,range, or app)
Dim Ndx As Long ' loop counter
Dim Pos1 As Integer ' used to find the Item specified in collection objects
Dim Pos2 As Integer ' used to find the Item specified in collection objects
Dim TempObj As Object

'
' split the object/property spec
'
PropArr = Split(PropertySpec, ".")
'
' If Rng is an object, then it must be a Range. That's the only
' type of object you pass from a cell.
'
If IsObject(TopObj) Then
    Set Obj = TopObj
Else
    '
    ' Otherwise, it better be one of the following strings. Else,
    ' blow up the user.
    '
    Select Case UCase(TopObj)
        Case "APP", "APPLICATION"
            Set Obj = Application
        Case "WB", "TWB", "THISWORKBOOK", "WORKBOOK"
            Set Obj = ThisWorkbook
        Case "WS", "TWS", "THISWORKSHEET", "WORKSHEET"
            Set Obj = Application.Caller.Parent
        Case Else
            GetInfo = CVErr(xlErrValue)
    End Select
End If

For Ndx = LBound(PropArr) To UBound(PropArr) - 1
    '
    ' this block of code is for handling items of a collection
    '
    Pos1 = InStr(1, PropArr(Ndx), "(")
    If Pos1 > 0 Then
        '
        ' if we've found the open paren, we're dealing with an
        ' item of a collection. now, find the closing paren.
        '
        Pos2 = InStr(1, PropArr(Ndx), ")")
        ItemSpec = Mid(PropArr(Ndx), Pos1 + 1, Pos2 - Pos1 - 1)
        If IsNumeric(ItemSpec) Then
            ' numeric -- going by index number
            ItemSpec = CLng(ItemSpec)
        Else
            ' string -- going by key name, so get rid of any quotes.
            ItemSpec = Replace(ItemSpec, """", "")
        End If
        '
        ' call the Item method of the collection object.
        '
        Set Obj = CallByName(Obj, Mid(PropArr(Ndx), 1, Pos1 - 1), _
            VbGet, ItemSpec)
    Else
        '
        ' we're not dealing with collections. just get the object.
        '
        Set Obj = CallByName(Obj, PropArr(Ndx), VbGet)
    End If
Next Ndx
'
' get the final property (typically 'name' or 'value' of the object tree)
'
If IsObject(Obj) Then
    GetInfo = CallByName(Obj, PropArr(UBound(PropArr)), VbGet)
End If

End Function

Public Function getArrayInfo(rng As Range, atr As String) As Variant
Dim temp As Excel.Range
Dim out() As Variant
Dim i As Long
i = 1

ReDim out(1 To rng.Rows.Count, 1 To 1)
Set temp = Intersect(rng, ActiveSheet.UsedRange)

For Each Item In temp.Cells
    out(i, 1) = GetInfo(Item, atr)
    i = i + 1
Next Item

getArrayInfo = out

End Function

将导入到模块后,现在需要向模块添加一个常规函数以使用数组公式。在getInfo函数之后添加此函数:

Public Function getArrayInfo(rng As Range, atr As String) As Variant
Dim temp As Excel.Range
Dim out() As Variant
Dim i As Long
i = 1

ReDim out(1 To rng.Rows.Count, 1 To 1)
Set temp = Intersect(rng, ActiveSheet.UsedRange)

For Each item In temp.Cells
    out(i, 1) = GetInfo(item, atr)
    i = i + 1
Next item

getArrayInfo = out

End Function
然后,在您的工作表中,您将获得以下模式:

=MODE(IF(getArrayInfo(data,"Interior.Color")=24,data))
其中数据是您的数据列。请记住使用Ctrl+Shift+enter将其作为数组公式输入

在这里,我使用以下数据集对其进行了测试:

替代解决方案: 此解决方案假定您能够稍微修改数据,特别是添加一个帮助器列并将范围转换为表,但更简单、运行更快且不需要VBA

一,。转到公式>定义名称>名称管理器

二,。单击New,随意命名,我选择了bg并在中引用:键入:

=GET.CELL63,INDIRECTrc[-1],FALSE

然后单击确定并关闭名称管理器

三,。选择数据表并转到“插入”>“表”>“表”,您将看到一个对话框,确认要选择的范围,检查表是否有标题,然后单击“确定”,您的数据现在应该具有表格式。这很容易识别,因为现在在表标题旁边应该有一个过滤器箭头

四,。在数据列右侧添加新标题,然后在标题中键入颜色。在该新列的第一个数据记录中,键入公式=bg或在步骤1中选择命名自定义命名范围的任何内容。单击enter,它将在整个列上使用相同的公式自动填充:

五,。最后,您有了一个helper列,它读取每个相应记录的colorIndex,因此您可以读取要分析的colorIndex,您的公式如下:

{=MODE.SNGLIFTable1[COLOR]=ColorIndex,Table1[INPUT]}其中ColorIndex是要分析的颜色的编号,例如在我的表格中,黄色是6,红色是3。请记住使用Ctrl+Shift+enter将其作为数组公式输入

在这里,您可以看到,我已经计算了数据表中所有颜色的对应模式,另外还有一个好处,即由于数据格式为表,因此在添加新记录时,我们的公式将自动更新


检查这个问题;我建议使用Chip Pearson提供的这个很棒的宏来获取各种单元格信息,包括颜色:谢谢,但我不想只获取单元格颜色,而是从具有特定颜色的单元格中获取数据。对不起,我遇到了一个名称错误=MODEIFGetInfoStats!B:B,内饰。颜色无,统计!B:B别介意,这个错误是固定的,但是我如何设置它应该从中提取数据的单元格颜色?首先,你需要知道你要分析的颜色索引,你可以用简单的GetInfocell,Interior.color找到,然后在我的原始公式中,而不是计算不等式xlNone,对这个特殊的colorIndex做一个相等的处理颜色索引是24,所以它应该类似于=MODEIFGetInfoStats!B:B,内饰。颜色24,统计!B:B对吗?不,它应该是一个等式,而不是不等式=。我将用一个例子来编辑我的答案。