如何在Excel中对行值进行sorting并在别处显示相应的列名?

基本上,我有这样的Excel表格 –

ABCD 16 14 13 12 14 14 13 14 16 14 15 12 14 14 15 14 

我想要Excel表格显示这个 –

  ABCD 16 14 13 12 ABCD 14 14 13 14 ABDC 16 14 15 12 ACBD 14 14 15 14 CABD 

换句话说,在每一行旁边,我想要列名的降序。 我如何做到这一点? 我尝试了几种不同的方式进行sorting,但是没有一个在另一个列中提供列名,就像我想要的那样。 请帮忙。

我使用Excel 2007。

编辑:

通过这篇文章后 ,我用了

 =INDEX(L$1:S$1,MATCH(LARGE(L42:S42,1),L42:S42,0)) =INDEX(L$1:S$1,MATCH(LARGE(L42:S42,2),L42:S42,0)) =INDEX(L$1:S$1,MATCH(LARGE(L42:S42,3),L42:S42,0)) =INDEX(L$1:S$1,MATCH(LARGE(L42:S42,4),L42:S42,0)) 

显示对应于每行最大4个值的列名。 但是,再次,当两个或更多的值是相等的,我得到这样的输出 –

  ABCD 16 14 13 12 ABCD 14 14 13 14 AAAC 16 14 15 12 ACBD 14 14 15 14 CAAA 

我如何解决这个问题以获得我想要的输出(我在开始时已经说过)?

在确定LARGE st值时,您必须考虑数值的位置。 如果数值相等,则必须确定列表中第一个相等的数值是否应该在第一位。

如果只有整数值,那么我们可以将这些值的位置作为小数位。

例:

在这里输入图像说明

公式在E2 ,复制到E2:H5

 {=INDEX($A$1:$D$1,MATCH(LARGE($A2:$D2+(10-COLUMN($A2:$D2))/10,E$1),$A2:$D2+(10-COLUMN($A2:$D2))/10,0))} 

这是一个数组公式。 input它没有大括号,并用[Ctrl] + [Shift] + [Enter]结束。

这仅适用于最多10列。 如果更多列(10-...)/10必须更改为(100-...)/100

在这个例子中,第一个相同的值也在列表中首先出现。 如果最后一个相同的数值应该在列表中最先出现,那么移除10-部分。

使用条件格式可以突出显示具有相同值的列名称。

  1. selectE2:H6
  2. 调用条件格式。
  3. 使用公式应用新规则:公式: =COUNTIF($A2:$D2,INDEX($A2:$D2,MATCH(LARGE($A2:$D2,E$1),$A2:$D2,0)))>1
  4. select你想要的格式。

答案已被接受,这是一个很好的答案,但只是为了我自己的娱乐,我会发表一个变化: –

 =INDEX($A$1:$D$1,MATCH(0,IF($A2:$D2=LARGE($A2:$D2,COLUMN()-COLUMN($E2)),COUNTIF($E2:E2,$A$1:$D$1),1),0)) 

这使用一个标准的COUNTIF / MATCH方法来消除已经input的字段,并且不会对数字的大小做任何假设。

再次,它是一个数组公式,并且必须使用Ctrl Shift Enterinput

在这里输入图像说明

这是一个VBA方法。 它将表中数据值的位置与列标签关联起来; 做一个稳定的数据值的sorting; 并返回列标签。

一个潜在的好处是,它可以用于文本或数字值; 如果需要,可以做成区分大小写。

它依赖于从A1开始的数据表,并且在A列或者第1行的工作表上没有其他的东西; 因为它使用这些范围来确定要处理的最后一行和一列。

要input这个macros(Sub), alt-F11打开Visual Basic编辑器。 确保您的项目在“项目浏览器”窗口中突出显示。

首先,从顶部菜单中selectInsert/Class Module并将下面的Class Module代码粘贴到打开的窗口中。 select类模块cRow命名它(按F4并更改Name属性) cRow

然后,从顶部的菜单中selectInsert/Module然后将常规模块代码粘贴到该窗口中。

要使用此macros(子),请确保您的数据显示在活动工作表上。 然后alt-F8打开macros对话框。 按名称selectmacros。

类模块

 Option Explicit Private pData As Variant Private pColLabel As String Public Property Get Data() As Variant Data = pData End Property Public Property Let Data(Value As Variant) pData = Value End Property Public Property Get ColLabel() As String ColLabel = pColLabel End Property Public Property Let ColLabel(Value As String) pColLabel = Value End Property 

常规模块

 Option Explicit Option Compare Text Sub SortAndColLabel() Dim vSrc As Variant, vRes() As Variant Dim cR As cRow, colR As Collection Dim LastRow As Long, LastCol As Long Dim R As Range Dim I As Long, J As Long LastRow = Cells(Rows.Count, "A").End(xlUp).Row LastCol = Cells(1, Columns.Count).End(xlToLeft).Column vSrc = Range(Cells(1, 1), Cells(LastRow, LastCol)) ReDim vRes(1 To UBound(vSrc, 1), 1 To UBound(vSrc, 2) * 2) 'Associate entries with column labels For I = 2 To UBound(vSrc, 1) Set colR = New Collection For J = 1 To UBound(vSrc, 2) Set cR = New cRow With cR .ColLabel = vSrc(1, J) .Data = vSrc(I, J) colR.Add cR End With Next J 'Sort the row, need to use stable sort CollectionBubbleSort colR 'Place in results array For J = 1 To colR.Count With colR(J) vRes(I, J) = vSrc(I, J) vRes(I, J + UBound(vSrc, 2)) = .ColLabel End With Next J Next I 'Add Column Labels to Vres For J = 1 To UBound(vSrc, 2) vRes(1, J) = vSrc(1, J) Next J 'Write the results Set R = Range("a1", Cells(UBound(vRes, 1), UBound(vRes, 2))) With R .EntireColumn.Clear .Value = vRes .HorizontalAlignment = xlCenter End With End Sub '--------------------------------------------------------------- 'Could use faster sort routine if necessary Sub CollectionBubbleSort(TempCol As Collection) 'Must manually insert element of collection to sort on in this version Dim I As Long Dim NoExchanges As Boolean ' Loop until no more "exchanges" are made. Do NoExchanges = True ' Loop through each element in the array. For I = 1 To TempCol.Count - 1 ' If the element is greater than the element ' following it, exchange the two elements. If TempCol(I).Data < TempCol(I + 1).Data Then NoExchanges = False TempCol.Add TempCol(I), after:=I + 1 TempCol.Remove I End If Next I Loop While Not (NoExchanges) End Sub