提示:其它语言是由 Google 机器翻译的。 你可以访问 English 版本。
登录
x
or
x
x
马上登记
x

or

如果Excel中的另一列中存在相同的值,如何连接单元格?

如果相同则合并

如左侧所示,如果第一列值相同,则需要连接第二列单元格。 你怎么能实现它?

在这篇文章中,我们将介绍三种实现它的方法。

如果公式和过滤条件相同,则连接单元格

如果与VBA代码具有相同的值,则将单元格连接起来

如果与Kutools for Excel具有相同的值,可轻松连接单元格


高级合并行:如果另一列中存在相同的值,则可以轻松合并单元格

您订购的 Kutools for Excel's 先进的组合行 实用程序,如果在Excel的另一列中存在相同的值,则可以轻松地将列中的单元格组合在一起。 看截图:

同样的污染物

Kutools for Excel:比200方便的Excel加载项,可以在60天免费试用。 下载并免费试用现在!

Office选项卡在Office中启用选项卡式编辑和浏览,使您的工作更轻松......
Kutools for Excel - 最佳办公生产力工具将解决您的大部分Excel问题
  • 重用任何东西: 将最常用或最复杂的公式,图表和其他任何内容添加到您的收藏夹中,并在将来快速重复使用它们。
  • 超过20文本功能: 从文本字符串中提取数字; 提取或删除部分文本; 将数字和货币转换为英语单词...
  • 合并工具:多个工作簿和表格合二为一; 合并多个单元格/行/列而不丢失数据; 合并重复行和总和...
  • 拆分工具:根据价值将数据拆分为多个表格; 一个工作簿到多个Excel,PDF或CSV文件; 一列到多列......
  • 粘贴跳过 隐藏/过滤行; 数和总和 按背景颜色; 创建邮件列表和 通过Cell的价值发送电子邮件...
  • 超级过滤器: 创建高级过滤方案并应用于任何工作表; 排序 按周,日,频率等; 筛选 通过大胆,公式,评论......
  • 超过300强大的功能; 适用于Office 2007-2019和365; 支持所有语言; 在公司轻松部署; 全功能60天免费试用。

如果公式和过滤条件相同,则连接单元格


使用下面的公式,如果另一列在Excel中包含相同的值,则可以连接相应的单元格内容。

1。 选择第二列以外的空白单元格(这里我们选择单元格C2),输入公式 = IF(A2 <> A1,B2,C1&“,”&B2) 进入公式栏,然后按 输入 键。

2。 然后选择单元格C2,然后将填充句柄向下拖动到需要连接的单元格。

3。 输入公式 = IF(A2 <> A3,CONCATENATE(A2, “”, “”,C2 “” “”), “”) 到单元格D2中,然后将填充句柄拖到其余单元格中。

4。 选择单元格D1,然后单击 数据 > 筛选。 看截图:

5。 单击单元格D1中的下拉箭头,取消选中 (空白) 框,然后单击 OK 按钮。

如果第一列值相同,您可以看到单元格被连接在一起。

注意:要成功使用上述公式,列A中的相同值必须连续。


如果与VBA代码具有相同的值,则将单元格连接起来

除了公式之外,在本节中,我们将向您展示如何使用VBA代码连接单元格,如果值相同。

1。 按 其他 + F11 键打开 Microsoft Visual Basic应用程序 窗口。

2。 在里面 Microsoft Visual Basic应用程序 窗口中,单击 插页 > 模块。 然后将以下代码复制并粘贴到 模块 窗口。

VBA代码:如果值相同,则连接单元格

Sub ConcatenateCellsIfSameValues()
	Dim xCol As New Collection
	Dim xSrc As Variant
	Dim xRes() As Variant
	Dim I As Long
	Dim J As Long
	Dim xRg As Range
	xSrc    = Range("A1", Cells(Rows.Count, "A").End(xlUp)).Resize(, 2)
	Set xRg = Range("D1")
	On Error Resume Next
	For I = 2 To UBound(xSrc)
		xCol.Add xSrc(I, 1), TypeName(xSrc(I, 1)) & CStr(xSrc(I, 1))
	Next I
	On Error GoTo 0
	ReDim xRes(1 To xCol.Count + 1, 1 To 2)
	xRes(1, 1) = "No"
	xRes(1, 2) = "Combined Color"
	For I = 1 To xCol.Count
		xRes(I + 1, 1) = xCol(I)
		For J = 2 To UBound(xSrc)
			If xSrc(J, 1) = xRes(I + 1, 1) Then
				xRes(I + 1, 2) = xRes(I + 1, 2) & ", " & xSrc(J, 2)
			End If
		Next J
		xRes(I + 1, 2) = Mid(xRes(I + 1, 2), 2)
	Next I
	Set xRg = xRg.Resize(UBound(xRes, 1), UBound(xRes, 2))
	xRg.NumberFormat = "@"
	xRg = xRes
	xRg.EntireColumn.AutoFit
End Sub

笔记:

1. D1 符合 设置xRg =范围(“D1”) 意味着结果将放置在单元格D1中。

2。 不和 组合的颜色 符合 xRes(1,1)=“否”和xRes(1,2)=“组合颜色” 是连接列的标题。 您可以根据需要更改它们。

3。 按 F5 键来运行代码,那么你将得到指定范围内的连接结果。


如果与Kutools for Excel具有相同的值,轻松地连接单元格(几次点击)

如果上述两种方法对你来说很复杂,下面我们将向你展示 先进的组合行 实用程序 Kutools for Excel. 如果另一列中存在相同的值,则可以通过逗号,分号等轻松地连接单元格。

Kutools for Excel :与超过300方便的Excel加载项, 免费试用60天无限制.

1。 选择您需要连接的单元格的范围,然后单击 Kutools > 内容 > 先进的组合行。 看截图:

2。 在里面 基于列组合行 对话框,您需要执行以下操作。

A.根据需要选择您想要连接单元格的值相同的列,然后单击 首要的关键 按钮。

B.选择要连接的列,单击 结合 按钮,然后从上下文菜单中指定一个分隔符(这里我们选择 逗号).

C.点击 OK 按钮。 看截图:

现在根据主键列来连接单元格。


连接多个单元格值并在单词之间添加空格或其他分隔符:

您订购的 Kutools for Excel's 结合 实用程序,您可以根据需要在Excel中快速将多行,列或单元格组合到列,行或具有特定分隔符的单元格中。 看截图:

同样的污染物

Kutools for Excel:比200方便的Excel加载项,可以在60天免费试用。 下载并免费试用现在!


如果与Kutools for Excel具有相同的值,可轻松连接单元格

Kutools for Excel 包含了比300更方便的Excel工具。 免费试用60天无限制。 立即下载免费试用!


Kutools for Excel - 最佳办公生产力工具提高80%的生产力

  • 重用: 快速插入 复杂的公式,图表 以及你以前用过的任何东西; 加密单元格 密码; 创建邮件列表 并发送电子邮件...
  • 超级方程式酒吧 (轻松编辑多行文字和公式); 阅读布局 (轻松读取和编辑大量单元格); 粘贴到过滤范围...
  • 合并单元格/行/列 不丢失数据; 分裂细胞含量; 组合重复的行/列...防止重复的细胞; 比较范围...
  • 选择复制或唯一 行; 选择空行 (所有细胞都是空的); 超级查找和模糊查找 在许多工作簿中; 随机选择......
  • 精确复制 多个单元格而不更改公式参考; 自动创建参考 多张表; 插入项目符号,复选框等等......
  • 提取文本,添加文本,按位置删除, 删除空间; 创建和打印分页小计; 在单元格内容和注释之间转换...
  • 超级过滤器 (将过滤方案保存并应用到其他工作表); 高级排序 按月/周/日,频率等; 特殊过滤器 用粗体,斜体......
  • 结合工作簿和工作表; 根据键列合并表; 将数据拆分为多个表格; 批量转换xls,xlsx和PDF...
  • 超过300强大的功能。 支持Office / Excel 2007-2019和365。 支持所有语言。 在您的企业或组织中轻松部署。 全功能60天免费试用。
kte tab 201905

Office选项卡为Office提供选项卡式界面,使您的工作更轻松

  • 在Word,Excel,PowerPoint中启用选项卡式编辑和阅读,Publisher,Access,Visio和Project。
  • 在同一窗口的新选项卡中打开并创建多个文档,而不是在新窗口中。
  • 通过50%提高您的工作效率,每天为您减少数百次鼠标点击!
官方底部
Say something here...
symbols left.
You are guest ( Sign Up? )
or post as a guest, but your post won't be published automatically.
Loading comment... The comment will be refreshed after 00:00.
  • To post as a guest, your comment is unpublished.
    Jade · 1 months ago
    Hi, first of all thanks for creating this resource. I have been trying to figure this out for a couple of hours and I'm stuck. I'm using your 'concatenate cells if same value' but my script is looking at column "D" instead of "A. I can't figure out how to get it to use a different column for the data besides the one right next to it. In my cases I want it to look at column "D" to see if the value is the same and if so, it will grab the data from column "H" and put that data from column "H' into a cell in column "J". How do I switch this to use column "H" for the data? Thx


    Sub ConcatenateCellsIfSameValues()
    Dim xCol As New Collection
    Dim xSrc As Variant
    Dim xRes() As Variant
    Dim I As Long
    Dim J As Long
    Dim xRg As Range
    xSrc = Range("D1", Cells(Rows.Count, "D").End(xlUp)).Resize(, 2)
    Set xRg = Range("J1")
    On Error Resume Next
    For I = 2 To UBound(xSrc)
    xCol.Add xSrc(I, 1), TypeName(xSrc(I, 1)) & CStr(xSrc(I, 1))
    Next I
    On Error GoTo 0
    ReDim xRes(1 To xCol.Count + 1, 1 To 2)
    xRes(1, 1) = "No"
    xRes(1, 2) = "Products"
    For I = 1 To xCol.Count
    xRes(I + 1, 1) = xCol(I)
    For J = 2 To UBound(xSrc)
    If xSrc(J, 1) = xRes(I + 1, 1) Then
    xRes(I + 1, 2) = xRes(I + 1, 2) & vbCrLf & xSrc(J, 2)
    End If
    Next J
    xRes(I + 1, 2) = Mid(xRes(I + 1, 2), 2)
    Next I
    Set xRg = xRg.Resize(UBound(xRes, 1), UBound(xRes, 2))
    xRg.NumberFormat = "@"
    xRg = xRes
    xRg.EntireColumn.AutoFit
    End Sub
  • To post as a guest, your comment is unpublished.
    Harry Vyvey · 3 months ago
    Hi,

    looks like 2 of your formulas are wrong :

    =IF(A2<>A3,CONCATENATE(A2,",""",C2,""""),""). You need to change "A2" to "D1". As you'll want to add the string to the previous cell.

    same goes for this formula :

    =IF(A2<>A1,B2,C1 & "," & B2) : Change C1 to D1.


    kind regards


    Harry
  • To post as a guest, your comment is unpublished.
    James · 11 months ago
    I use this VBA for lots of my spreadsheets and its great. But the spreadsheets have become very large 50k+ rows and it doesnt seem to be working any more. If I use it on 1000 rows it works fine but large sets of data it cant seem to cope with. No errors just no results. Any help would be appreciated.
    • To post as a guest, your comment is unpublished.
      crystal · 10 months ago
      Hi James,
      I tested the code as you mentioned, but it still works well in my case even I set the rows to 1000+.
  • To post as a guest, your comment is unpublished.
    Matt Carrington · 1 years ago
    Using the VBA macro and getting great results, I have tried tweaking it slightly for my needs but cant get it to work so I hope you can help.

    Which bit do I change to make it concat a specific column, not the one directly to the right of the xSrc = Range?

    Thanks for your great work!
    • To post as a guest, your comment is unpublished.
      Matt Carrington · 1 years ago
      Or as a better option, if you had 3 columns instead of 2 and found duplicates in column A (like your example) can you concat column B into a cell and column C into a seperate cell? So if you had columns of Number, Colour, Age, could you concat colour and age into different columns upon finding duplicates in Number? Hope that makes sense!
  • To post as a guest, your comment is unpublished.
    Ram · 1 years ago
    This is exactly what I needed...thank you
  • To post as a guest, your comment is unpublished.
    RS · 1 years ago
    I am so thankful to you for these formulas. Fantastic work!
  • To post as a guest, your comment is unpublished.
    Marissa W · 1 years ago
    The concatenate feature does not work. Combines all the rows into one cell despite different keys.
    • To post as a guest, your comment is unpublished.
      crystal · 1 years ago
      Dear Marissa,
      Sorry I am not sure I got your question. There are three methods in the article, which concatenate feature do you mean?
  • To post as a guest, your comment is unpublished.
    Geoffrey saya · 2 years ago
    How can i merge collums with the same names eg Column one reads Kenya throughout and collumn 2 reads western, Nyanza( appearing more than ten times) and column 3 three names of districts. I want to get how mang districts do we have in every province but keep the columns.
    • To post as a guest, your comment is unpublished.
      crystal · 2 years ago
      Dear Geoffrey,

      Please try the Advanced Combine Rows utility we mentioned in above method two. If you want to keep the columns, please make a copy of the original range and do everything in the copied range.

      Specify the province column as Primary Key, set the Column 1 as Keep 1st, and then set the district column as Calculate > Count.

      After that, you will get how many district you have in every province.
  • To post as a guest, your comment is unpublished.
    cathy postmus · 3 years ago
    Can't seem to concatenate with more than 2 cells using formulas and filters