Index column header where max count of values in c

2019-09-02 08:56发布

问题:

I have a table with nine columns and twenty rows. I am trying to id which column header has the max count of fields below it where the values are greater than or equal to a specified number. The table range is J4:R23. The specified number is B6. J4:R4 contains the header that i am trying to match. Any thoughts on formula to obtain the appropriate column header? Thanks!

回答1:

This thing barely fits, certainly not the best solution but should work jus fine:

=IF(SUMIFS($J$5:$J$23;">"&$B$6)=MAX(
SUMIFS($J$5:$J$23;">"&$B$6);
SUMIFS($K$5:$K$23;">"&$B$6);
SUMIFS($L$5:$L$23;">"&$B$6);
SUMIFS($M$5:$M$23;">"&$B$6);
SUMIFS($N$5:$N$23;">"&$B$6);
SUMIFS($O$5:$O$23;">"&$B$6);
SUMIFS($P$5:$P$23;">"&$B$6);
SUMIFS($Q$5:$Q$23;">"&$B$6);
SUMIFS($R$5:$R$23;">"&$B$6));$J$4;
IF(SUMIFS($K$5:$K$23;">"&$B$6)=MAX(
SUMIFS($J$5:$J$23;">"&$B$6);
SUMIFS($K$5:$K$23;">"&$B$6);
SUMIFS($L$5:$L$23;">"&$B$6);
SUMIFS($M$5:$M$23;">"&$B$6);
SUMIFS($N$5:$N$23;">"&$B$6);
SUMIFS($O$5:$O$23;">"&$B$6);
SUMIFS($P$5:$P$23;">"&$B$6);
SUMIFS($Q$5:$Q$23;">"&$B$6);
SUMIFS($R$5:$R$23;">"&$B$6));$K$4;
IF(SUMIFS($L$5:$L$23;">"&$B$6)=MAX(
SUMIFS($J$5:$J$23;">"&$B$6);
SUMIFS($K$5:$K$23;">"&$B$6);
SUMIFS($L$5:$L$23;">"&$B$6);
SUMIFS($M$5:$M$23;">"&$B$6);
SUMIFS($N$5:$N$23;">"&$B$6);
SUMIFS($O$5:$O$23;">"&$B$6);
SUMIFS($P$5:$P$23;">"&$B$6);
SUMIFS($Q$5:$Q$23;">"&$B$6);
SUMIFS($R$5:$R$23;">"&$B$6));$L$4;
IF(SUMIFS($M$5:$M$23;">"&$B$6)=MAX(
SUMIFS($J$5:$J$23;">"&$B$6);
SUMIFS($K$5:$K$23;">"&$B$6);
SUMIFS($L$5:$L$23;">"&$B$6);
SUMIFS($M$5:$M$23;">"&$B$6);
SUMIFS($N$5:$N$23;">"&$B$6);
SUMIFS($O$5:$O$23;">"&$B$6);
SUMIFS($P$5:$P$23;">"&$B$6);
SUMIFS($Q$5:$Q$23;">"&$B$6);
SUMIFS($R$5:$R$23;">"&$B$6));$M$4;
IF(SUMIFS($N$5:$N$23;">"&$B$6)=MAX(
SUMIFS($J$5:$J$23;">"&$B$6);
SUMIFS($K$5:$K$23;">"&$B$6);
SUMIFS($L$5:$L$23;">"&$B$6);
SUMIFS($M$5:$M$23;">"&$B$6);
SUMIFS($N$5:$N$23;">"&$B$6);
SUMIFS($O$5:$O$23;">"&$B$6);
SUMIFS($P$5:$P$23;">"&$B$6);
SUMIFS($Q$5:$Q$23;">"&$B$6);
SUMIFS($R$5:$R$23;">"&$B$6));$N$4;
IF(SUMIFS($O$5:$O$23;">"&$B$6)=MAX(
SUMIFS($J$5:$J$23;">"&$B$6);
SUMIFS($K$5:$K$23;">"&$B$6);
SUMIFS($L$5:$L$23;">"&$B$6);
SUMIFS($M$5:$M$23;">"&$B$6);
SUMIFS($N$5:$N$23;">"&$B$6);
SUMIFS($O$5:$O$23;">"&$B$6);
SUMIFS($P$5:$P$23;">"&$B$6);
SUMIFS($Q$5:$Q$23;">"&$B$6);
SUMIFS($R$5:$R$23;">"&$B$6));$O$4;
IF(SUMIFS($P$5:$P$23;">"&$B$6)=MAX(
SUMIFS($J$5:$J$23;">"&$B$6);
SUMIFS($K$5:$K$23;">"&$B$6);
SUMIFS($L$5:$L$23;">"&$B$6);
SUMIFS($M$5:$M$23;">"&$B$6);
SUMIFS($N$5:$N$23;">"&$B$6);
SUMIFS($O$5:$O$23;">"&$B$6);
SUMIFS($P$5:$P$23;">"&$B$6);
SUMIFS($Q$5:$Q$23;">"&$B$6);
SUMIFS($R$5:$R$23;">"&$B$6));$P$4;
IF(SUMIFS($Q$5:$Q$23;">"&$B$6)=MAX(
SUMIFS($J$5:$J$23;">"&$B$6);
SUMIFS($K$5:$K$23;">"&$B$6);
SUMIFS($L$5:$L$23;">"&$B$6);
SUMIFS($M$5:$M$23;">"&$B$6);
SUMIFS($N$5:$N$23;">"&$B$6);
SUMIFS($O$5:$O$23;">"&$B$6);
SUMIFS($P$5:$P$23;">"&$B$6);
SUMIFS($Q$5:$Q$23;">"&$B$6);
SUMIFS($R$5:$R$23;">"&$B$6));$Q$4;
IF(SUMIFS($Q$5:$Q$23;">"&$B$6)=MAX(
SUMIFS($J$5:$J$23;">"&$B$6);
SUMIFS($K$5:$K$23;">"&$B$6);
SUMIFS($L$5:$L$23;">"&$B$6);
SUMIFS($M$5:$M$23;">"&$B$6);
SUMIFS($N$5:$N$23;">"&$B$6);
SUMIFS($O$5:$O$23;">"&$B$6);
SUMIFS($P$5:$P$23;">"&$B$6);
SUMIFS($Q$5:$Q$23;">"&$B$6);
SUMIFS($R$5:$R$23;">"&$B$6));$R$4;
"Error in process"
)
)
)
)
)
)
)
)
)

See my attached file (replacing and copying may have corrupted the formula).



回答2:

I made a VBA function too, it's redundant and so on but here it is:

Public Function FINDHEADERWHEREMAXCOUNTIFS(Target As Range, Condition As Double)
Dim rng As Range
NumCols = Target.Columns.Count 'counts how many header values we can choose of
Dim Headers() 'defines separate arrays for headers and values (turned out to be obsolete, see variable x)
    ReDim Headers(1 To NumCols)
Dim ValuesArr()
    ReDim ValuesArr(1 To NumCols)

HeaderRow = Target.Row 'row in which headers are located
LastRow = HeaderRow + Target.Rows.Count - 1 'last row with values
FirstColumn = Target.Column 'first column with values
LastColumn = FirstColumn + Target.Columns.Count - 1 'last column with values

For k = FirstColumn To LastColumn 'for each column
    i = i + 1 'set array position
    Headers(i) = Cells(HeaderRow, k).Value2 'set the corresponding header
    For Each rng In Range(Cells(HeaderRow, k), Cells(LastRow, k)) 'for each value
        If rng.Row <> HeaderRow Then 'I mean value, not header
            If rng.Value2 > Condition Then ValuesArr(i) = ValuesArr(i) + 1 'if it's higher than the condition then count
        End If
    Next
Next

x = 1 'default is the first header
For j = 1 To NumCols 'for each header
 If ValuesArr(j) > ValuesArr(x) Then x = j 'if the corresponding value is larger then standard then change the current one to standard
Next
FINDHEADERWHEREMAXCOUNTIFS = Headers(x) 'the standard value's corresponding header is the output
End Function