如果值匹配,将单元格数据连接到另一个数据 [英] Concatenate cell data into another data if values matches

查看:141
本文介绍了如果值匹配,将单元格数据连接到另一个数据的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在同一个Excel表中有两个列A和B 。我正在尝试,如果在列B 两个值匹配,那么它应该复制相同的值 A 在同一行。 p>

例如



 code>列A列B 
xyz 1
abc 1
pqr 1
eee 2
qqq 3
www 4
oop 5

Desierd输出

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


解决方案

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

 公共功能conditional_concat_strs(rSTRs As Range,rCRITs As Range,rCRIT As Range,optional sDELIM As String =,)
Dim c As Long,sTMP As String

设置rSTRs = rSTRs.Cells(1,1).Resize(rCRITs.Rows.Count,rCRITs.Columns。计数)
对于c = 1到rCRITs.Cells.Count
如果rCRITs(c).Value2 = rCRIT然后_
sTMP = sTMP& rSTR(c).Value& sDELIM
下一步c
conditional_concat_strs = Left(sTMP,Application.Max(Len(sTMP) - Len(sDELIM),0))
结束函数

使用像任何本地工作表函数。




I have two columns A and B in same excel sheet. I am trying that if in Column B two values matches then it should copy related value A in same row.

For e.g

Table

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

Desierd Output

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

解决方案

You could probably use a User Defined Function (aka UDF) for this. Put this into a module sheet.

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

Use like any native worksheet function.

      

这篇关于如果值匹配,将单元格数据连接到另一个数据的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆