宏,用于合并和连接Excel中的单元格以查找其他列信息匹配的行 [英] Macro to merge and concatenate cells in Excel for rows in which information on other columns matches
问题描述
抱歉,这是一个普通的帖子,但是找不到完全适用于我的内容。
apologize if this is a common post, but couldn't find something that fully applies to me.
它与(与之非常相似的帖子除外,除了合并两个单元格之外,我还希望合并和连接):
在Excel for在其他列中的信息匹配的行中
It's a very similar post to (except that instead of just merging the 2 cells, i am looking to merge and concatenate): Macro to merge cells in Excel for rows in which information in other columns matches
参考上面文章中的图像,我正在寻找的是要合并的单元格P2和P3,要串联的数据。例如:如果P2具有abc,而P3具有xyz,我正在寻找最终产品在合并单元格中为abcxyz。
Referencing the image in the post above, what i am looking for are cells P2 and P3 to be merged and the data to be concatenated. For eg: if P2 had abc, and P3 had xyz, i am looking for end product to be abcxyz in the merged cell.
任何帮助将不胜感激。我所拥有的仅使我能够合并,但是我不确定如何将其合并。
Any help would be greatly appreciated. What i have only enables me to merge, but i am not sure how to concatenate it.
Sub Main()
Sub Main()
Dim i As Long
Dim j As Long
Dim sameRows As Boolean
sameRows = True
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
For j = 1 To 6
If StrComp(Cells(i, j), Cells(i + 1, j), vbTextCompare) Then
sameRows = False
End If
Next j
If sameRows Then
Range(Cells(i, 7), Cells(i + 1, 7)).Merge
End If
sameRows = True
Next i
结束子段
推荐答案
只需在代码中添加以下行:
simply add this line in the code:
Cells(i, 7).Value = Cells(i, 16).Text + Cells(i + 1, 16).Text
完整代码:
Sub merge()
Dim i As Long
Dim j As Long
Dim sameRows As Boolean
sameRows = True
For i = 1 To Range("A" & Rows.Count).End(xlUp).Row
For j = 1 To 6
If StrComp(Cells(i, j), Cells(i + 1, j), vbTextCompare) Then
sameRows = False
End If
Next j
If sameRows Then
Range(Cells(i, 7), Cells(i + 1, 7)).merge
Cells(i, 7).Value = Cells(i, 16).Text + Cells(i + 1, 16).Text
End If
sameRows = True
Next i
End Sub
这篇关于宏,用于合并和连接Excel中的单元格以查找其他列信息匹配的行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!