【问题标题】:Concatenate cell data into another data if values matches如果值匹配,则将单元格数据连接到另一个数据中
【发布时间】:2016-05-22 07:47:43
【问题描述】:

我在同一个 Excel 表中有两个 columns A and B。我正在尝试,如果在Column B 中有两个值匹配,那么它应该在同一行中复制相关值A

例如

表格

Column A      Column B
xyz              1
abc              1
pqr              1  
eee              2
qqq              3
www              4
oop              5

期望输出

column A         Column B
xyz,abc,pqr         1
eee                 2
qqq                 3
www                 4
oop                 5 

【问题讨论】:

    标签: excel vba


    【解决方案1】:

    您可能会为此使用用户定义函数(又名 UDF)。将其放入模块表中。

    Public Function conditional_concat_strs(rSTRs As Range, rCRITs As Range, rCRIT As Range, Optional sDELIM As String = ", ")
        Dim c As Long, sTMP As String
    
        Set rSTRs = rSTRs.Cells(1, 1).Resize(rCRITs.Rows.Count, rCRITs.Columns.Count)
        For c = 1 To rCRITs.Cells.Count
            If rCRITs(c).Value2 = rCRIT Then _
                sTMP = sTMP & rSTRs(c).Value & sDELIM
        Next c
        conditional_concat_strs = Left(sTMP, Application.Max(Len(sTMP) - Len(sDELIM), 0))
    End Function
    

    像任何本机工作表函数一样使用。

          

    【讨论】:

    • 很抱歉听到这个消息。我提供的屏幕截图没有经过 Photoshop 处理。
    • 我知道,,,我可以把我的 excel 表给你吗,问题是它适用于整行\
    • 欢迎您将经过编辑的示例工作表发布到任何公开可用的文件上传区域,然后编辑您的问题以包含每个人的工作簿链接。希望问题与合并的单元格无关。
    • @Jeeped 我正要回复,然后注意到这个问题是在去年 8 月提出的,并且因您编辑标签而受到影响。我也看到其他问题也这样做了,但不明白原因。
    • @Jeeped 为什么不把conditional_concat_strs = Mid(sTMP, Len(sDELIM) + 1)放在最后?
    【解决方案2】:

    你也可以用这个:

    Public Sub combine()
    
        Dim row, result, lastRow As Integer
        Dim isExist As Boolean
    
        With Sheets("sheetname")
    
            'get the last use row
            lastRow = .Range("A1").SpecialCells(xlCellTypeLastCell).row
    
            'Loop from row 1 to last row
            For row = 1 To lastRow Step 1
    
                'set the start row for result.
                result = 1
    
                'Reset flag
                isExist = False
    
                'Loop result count column until blank
                Do While .Range("F" & result) <> ""
    
                    'check count
                    If .Range("B" & row) = .Range("F" & result) Then
    
                        isExist = True
    
                        'If old, combine
                        .Range("E" & result) = .Range("E" & result) & "," & .Range("A" & row)
    
                        Exit Do
    
                    End If
    
                    'increase row
                    result = result + 1
    
                Loop
    
                'If new, add new record
                If Not isExist Then
                    .Range("E" & result) = .Range("A" & row)
                    .Range("F" & result) = .Range("B" & row)
                End If
    
            Next row
    
        End With
    
    End Sub
    

    在这里,我的代码的测试证据:

    我使用column A &amp; B 作为输入,column E &amp; F 作为输出。

    如果有任何问题,请告诉我。

    【讨论】:

      【解决方案3】:

      也有一个公式解决方案(带有辅助列):

      假设数据是列 A:B ...

      • 在 C1 中写下这个公式:=IF(A1&lt;&gt;A2,B2,D1&amp;","&amp;B2)
      • 在 D1 中写下这个公式:=IF(A2&lt;&gt;A3,A2,"")
      • 过滤 D 列的空白,然后删除可见单元格。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 2019-03-25
        • 1970-01-01
        • 2020-01-06
        • 1970-01-01
        • 2022-08-05
        • 1970-01-01
        相关资源
        最近更新 更多