簡體   English   中英

查找重復次數最多的值,但僅在 excel 中某些顏色的單元格中

[英]Find the most repeated value but only in cells of certain color in 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 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 function導入模塊后,您現在需要向模塊添加額外的 function 以使用數組公式。 在getInfo 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

然后,在您的工作表中,您將獲得以下模式:

=MODE(IF(getArrayInfo(data,"Interior.Color")=24,data))

其中data是您的數據列。 請記住使用Ctrl + Shift + Enter將其作為數組公式輸入

在這里,我用這個數據集對其進行了測試:

示例實現

替代解決方案:

此解決方案假定您能夠稍微修改數據,特別是添加幫助列並將范圍轉換為表格,但更簡單,運行速度更快,並且不需要 VBA。

1. Go 到Formulas > Defined Names > Name Manager 帶下划線的名稱管理器的公式功能區

2.點擊New ,任意命名,我選擇了“bg”,在“Refers to:”中輸入:

=GET.CELL(63,INDIRECT("rc[-1]",FALSE))

名稱管理器窗口輸入

然后單擊確定並關閉名稱管理器。

3. Select 你的數據表和 go 到Insert > Tables > Table ,你會看到一個對話框來確認你想要 select 的范圍,如果你的數據現在應該有表頭,請格式化。 它很容易識別,因為現在您的表格 header 旁邊應該有一個過濾箭頭。

具有表格格式的數據

4.在數據列的右側添加一個新的 header,然后在 header 中鍵入color 在此新列的第一個數據記錄中,鍵入公式=bg (或您在步驟 1 中選擇命名自定義命名范圍的任何內容)。 單擊輸入,它應該在整個列上使用相同的公式自動填充:

具有自動填充輔助列的數據表

5.現在,最后,您有一個輔助列,可以讀取每個相應記錄的 colorIndex,因此您可以讀取要分析的 colorIndex,並且您的公式很簡單:

{=MODE.SNGL(IF(Table1[COLOR]=ColorIndex,Table1[INPUT]))}其中colorIndex是您要分析的顏色的編號,例如在我的表中黃色是 6,紅色是 3。記住使用Ctrl + Shift + Enter將其作為數組公式輸入

在這里你可以看到我已經為我的數據表中的所有colors計算了對應的模式,另外的好處是由於數據被格式化為表格,當我們添加新記錄時我們的公式會自動更新

最終實施的解決方案

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM