我有一个类似于以下(输入)的表:
_element___|_color__
494 | red
592 | blue
493 | red
139 | green
393 | blue
496 | black
我希望得到一个看起来像这样的表(输出):
_color_____|_elements__
red | 493, 494
blue | 393, 592
green | 139
black | 496
我也会接受一个得到以下输出的答案:
_color_____|_element__|_element__|_element__|
red | 493 | 494 | |
blue | 393 | 592 | |
green | 139 | | |
black | 496 | | |
我从这个answer知道如何让左边的列“颜色”完成。我不知道如何使右边的列“元素”完成 - 顺序无关紧要。
我更喜欢没有宏,但是宏也很好! - 我不知道足够的Excel判断哪个是最好的。我还需要一个更好的标题来解决这个问题。
___EDIT____
通过@ Gary's Student的答案代码,我在下面做了这个函数:
Function GetElement(elements As Range, colors As Range, color As Range) As String
Dim i As Long
Dim Nels As Long: Nels = elements.Count + 1
Dim str As String: str = ""
If IsError(Cells(color.Row, color.Column).Value) Then
GetElement = ""
Exit Function
End If
For i = 2 To Nels
If Cells(color.Row, color.Column).Value = Cells(i, colors.Column).Value Then
If str = "" Then
str = Cells(i, elements.Column).Value
Else
str = str & ", " & Cells(i, elements.Column).Value
End If
End If
Next i
GetElement = str
End Function
在“颜色”右侧列的单元格上使用此功能,使用f4
将第一个和第二个参数锁定到列的范围“元素“和右侧”颜色“得到了正确的
答案 0 :(得分:0)
如果我们开始:
并运行这个短宏:
Sub TableOrganizer()
Dim st As String, i As Long, N As Long
Dim M As Long, j As Long
Range("B:B").Copy Range("C1")
Range("C:C").RemoveDuplicates Columns:=1, Header:=xlYes
N = Cells(Rows.Count, "C").End(xlUp).Row
M = Cells(Rows.Count, "A").End(xlUp).Row
For i = 2 To N
st = Cells(i, "C").Value
t = ""
For j = 2 To M
If st = Cells(j, "B").Value Then
t = t & "," & Cells(j, "A").Value
End If
Next j
Cells(i, "D").Value = "'" & Mid(t, 2)
Next i
End Sub
我们最终会: