2016-04-24 200 views
0

我有这张表;如何在Excel中的特定单元格中组合列值?

Table enter image description here

我希望能够列出这些选项与选项价值观在一个单元格像这样每一个给定的产品ID:Image enter image description here

还要记住我在该表中列出了1800个产品ID,因此手动提取数据非常困难。

谢谢。

+0

请显示您的代码 –

+0

我试图结合一些基本功能无济于事,我目前在我的桌子上没有代码。 –

回答

0

这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 –

0

如果在Office 365最新更新或使用在线应用程序,你可以使用以下数组公式:

="Size:" & TEXTJOIN("|",TRUE,IF(($A$2:$A$12=E2)*($B$2:$B$12="Size"),$C$2:$C$12,"")) & ";Colour:" & TEXTJOIN("|",TRUE,IF(($A$2:$A$12=E2)*($B$2:$B$12="Colour"),$C$2:$C$12,"")) 

作为数组公式,它必须与确认退出时按Ctrl-Shift键输入编辑模式而不是输入或制表符。如果做得好,Excel将把公式编号为{}

enter image description here

0

请尝试此方法。

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 
相关问题