如何在Excel中的特定单元格中组合列值?
问题描述:
我有这张表;如何在Excel中的特定单元格中组合列值?
我希望能够列出这些选项与选项价值观在一个单元格像这样每一个给定的产品ID:Image
还要记住我在该表中列出了1800个产品ID,因此手动提取数据非常困难。
谢谢。
答
这UDF会给你一定的产品ID有一定的选择您所需的输出:
Function UDF_ListValues(prodId As Variant, attrib As String, R As Range)
Dim colValues As Collection
Dim curRow As Range
Dim found As Boolean
Dim value As Variant
Dim first As Boolean
If R.Columns.Count <> 3 Then
UDF_ListValues = xlErrNA
Else
Set colValues = New Collection
For Each curRow In R.Rows
If curRow.Cells(1, 1).value = prodId And curRow.Cells(1, 2).value = attrib Then
found = False
For Each value In colValues
If curRow.Cells(1, 3).value = value Then
found = True
Exit For
End If
Next
If Not found Then colValues.Add curRow.Cells(1, 3)
End If
Next
first = True
For Each value In colValues
If first Then
UDF_ListValues = attrib & ":"
first = False
Else
UDF_ListValues = UDF_ListValues & "¦"
End If
UDF_ListValues = UDF_ListValues & value
Next
Set colValues = Nothing
End If
End Function
要在你的榜样得到确切的输出一样,你可以按如下调用细胞F2这个功能:
= UDF_ListValues(E2, "Size", $A$2:$C$22) & ";" & UDF_ListValues(E2, "Colour", $A$2:$C$22)
或者您可以轻松修改代码以自动列出所有发生的选项。
+0
非常感谢,作品像魅力。 @NiH –
答
请尝试此方法。
Sub Macro()
Dim lngRow As Long
For lngRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
If StrComp(Range("A" & lngRow), Range("A" & lngRow - 1), vbTextCompare) = 0 Then
If Range("B" & lngRow) <> "" Then
Range("B" & lngRow - 1) = Range("B" & lngRow - 1) & "|" & Range("B" & lngRow)
End If
Rows(lngRow).Delete
End If
Next
End Sub
请显示您的代码 –
我试图结合一些基本功能无济于事,我目前在我的桌子上没有代码。 –