根据给定的标准对值求和后如何返回标准?

时间:2019-11-14 21:05:39

标签: excel

我有一张类似的纸:

enter image description here

因此,将A列和B列以及C列中的数字组合在一起。我想做的是将各列中的每个值相加(例如:每次出现“ Cat”时都添加C列,并且“狗”和“草”等),然后在A和B列中找到最高的值,然后返回该值。因此,例如,在上面的示例中,Dog将成为公式结果,因为它的C列总计为28。是否有一个公式(或者很可能是公式的组合)可以实现这一目标?

谢谢!

2 个答案:

答案 0 :(得分:1)

仅显示公式为:

=INDEX(INDEX(INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),N(IF({1},MODE.MULT(IF(ROW($1:$24)=MATCH(INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),0),ROW($1:$24)*{1,1}))))),MATCH(MAX(SUMIFS(C:C,A:A,INDEX(INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),N(IF({1},MODE.MULT(IF(ROW($1:$24)=MATCH(INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),0),ROW($1:$24)*{1,1}))))))+SUMIFS(C:C,B:B,INDEX(INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),N(IF({1},MODE.MULT(IF(ROW($1:$24)=MATCH(INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),0),ROW($1:$24)*{1,1}))))))),SUMIFS(C:C,A:A,INDEX(INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),N(IF({1},MODE.MULT(IF(ROW($1:$24)=MATCH(INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),0),ROW($1:$24)*{1,1}))))))+SUMIFS(C:C,B:B,INDEX(INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),N(IF({1},MODE.MULT(IF(ROW($1:$24)=MATCH(INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),INDEX(A1:B12,N(IF({1},INT((ROW($1:$24)-1)/2)+1)),N(IF({1},MOD((ROW($1:$24)-1),2)+1))),0),ROW($1:$24)*{1,1})))))),0))

这是一个数组公式,退出编辑模式时必须使用Ctrl-Shift-Enter而不是Enter进行确认。

enter image description here


通过新的动态数组公式,它变得更加易于管理:

=INDEX(UNIQUE(INDEX(A1:B12,N(IF({1},INT(SEQUENCE(24,,0)/2)+1)),N(IF({1},MOD(SEQUENCE(24,,0),2)+1)))),MATCH(MAX(SUMIFS(C:C,A:A,UNIQUE(INDEX(A1:B12,N(IF({1},INT(SEQUENCE(24,,0)/2)+1)),N(IF({1},MOD(SEQUENCE(24,,0),2)+1)))))+SUMIFS(C:C,B:B,UNIQUE(INDEX(A1:B12,N(IF({1},INT(SEQUENCE(24,,0)/2)+1)),N(IF({1},MOD(SEQUENCE(24,,0),2)+1)))))),SUMIFS(C:C,A:A,UNIQUE(INDEX(A1:B12,N(IF({1},INT(SEQUENCE(24,,0)/2)+1)),N(IF({1},MOD(SEQUENCE(24,,0),2)+1)))))+SUMIFS(C:C,B:B,UNIQUE(INDEX(A1:B12,N(IF({1},INT(SEQUENCE(24,,0)/2)+1)),N(IF({1},MOD(SEQUENCE(24,,0),2)+1))))),0))

enter image description here


但是我们可以在动态数组公式中使用辅助列。

在一个单元格中,我们放置:

=UNIQUE(INDEX(A1:B12,N(IF({1},INT(SEQUENCE(24,,0)/2)+1)),N(IF({1},MOD(SEQUENCE(24,,0),2)+1))))

我把它放在E1中,然后用sumifs来引用它:

=SUMIFS(C:C,A:A,E1#)+SUMIFS(C:C,B:B,E1#)

我将其放在F1中,然后使用INDEX / MATCH:

=INDEX(E1#,MATCH(MAX(F1#),F1#,0))

enter image description here


使用常规公式做很长的路要走,一个人需要将两列复制粘贴到另一个下方,然后使用“数据”标签上的“删除重复项”来获得唯一列表:

enter image description here

然后使用F1中的公式:

=SUMIFS(C:C,B:B,E1)+SUMIFS(C:C,A:A,E1)

然后复制列表。然后使用INDEX / MATCH:

=INDEX(E:E,MATCH(MAX(F:F),F:F,0))

返回所需的值。

enter image description here


这里要彻底讲解,为什么vba对此更好。将此放入模块:

Function myMatch(RngA As Range, RngB As Range, sumRng As Range)
    If RngA.Cells.Count <> RngB.Cells.Count Or RngA.Cells.Count <> sumRng.Cells.Count Or RngB.Cells.Count <> sumRng.Cells.Count Then
        myMatch = CVErr(xlErrValue)
        Exit Function
    End If
    Dim arrA As Variant
    arrA = RngA.Value

    Dim arrB As Variant
    arrB = RngB

    Dim dict As Object
    Set dict = CreateObject("Scripting.Dictionary")

    Dim j As Long
    For j = 1 To 2
        Dim i As Long
        For i = 1 To UBound(arrA)
            Dim uRec As String
            uRec = IIf(j = 1, arrA(i, 1), arrB(i, 1))

            Dim smRec As Double
            smRec = Application.SumIfs(sumRng, RngA, IIf(j = 1, arrA(i, 1), arrB(i, 1))) + Application.SumIfs(sumRng, RngB, IIf(j = 1, arrA(i, 1), arrB(i, 1)))
            On Error Resume Next
                dict.Add uRec, smRec
            On Error GoTo 0
        Next i
    Next j

    Dim mx As Double
    mx = 0

    Dim temp As String
    temp = ""

    Dim key As Variant
    For Each key In dict.Keys
        If dict(key) > mx Then
            temp = key
            mx = dict(key)
        End If
    Next key

    myMatch = temp
End Function

然后,您需要在工作表上进行的所有操作就是将其称为常规函数,列出三个区域:

=myMatch(A1:A12,B1:B12,C1:C12)

enter image description here

答案 1 :(得分:0)

您还可以使用 Power Query 解决问题。

步骤是:

  1. 将您的源数据(这是三列表)添加到Power Query Editor;

Step1

  1. 使用Transform标签下的 Merge Columns 合并列功能,以分号(例如分号;)合并前两列;

Step 2

  1. 使用Transform标签下的拆分列函数,用相同的定界符(例如分号;)拆分合并的列,并确保在高级选项中选择将结果放入;

Split Columns

  1. 使用分组依据函数按Merged列对最后一列进行分组,并将SUM设置为该函数;

Step 4

  1. 最后,排序最后一列降序

Step 5

您可以关闭并加载结果到新的工作表中(默认情况下)。

以下是强大的查询M代码,仅供参考。所有步骤都使用内置的功能,这些功能简单明了且易于执行。

let
    Source = Excel.CurrentWorkbook(){[Name="Table2"]}[Content],
    #"Changed Type" = Table.TransformColumnTypes(Source,{{"Column1", type text}, {"Column2", type text}, {"Column3", Int64.Type}}),
    #"Merged Columns" = Table.CombineColumns(#"Changed Type",{"Column1", "Column2"},Combiner.CombineTextByDelimiter(";", QuoteStyle.None),"Merged"),
    #"Split Column by Delimiter" = Table.ExpandListColumn(Table.TransformColumns(#"Merged Columns", {{"Merged", Splitter.SplitTextByDelimiter(";", QuoteStyle.Csv), let itemType = (type nullable text) meta [Serialized.Text = true] in type {itemType}}}), "Merged"),
    #"Changed Type1" = Table.TransformColumnTypes(#"Split Column by Delimiter",{{"Merged", type text}}),
    #"Grouped Rows" = Table.Group(#"Changed Type1", {"Merged"}, {{"Sum", each List.Sum([Column3]), type number}}),
    #"Sorted Rows" = Table.Sort(#"Grouped Rows",{{"Sum", Order.Descending}})
in
    #"Sorted Rows"

如果您有任何疑问,请告诉我。干杯:)