VBA 指定某列相同的内容合并,对应的其它列内容相加

合并需求

假如某一列包含多种重复单元,但是重复的单元对应的其它列却不相同,需求便是指定某一列,寻找其中相同的元素,对两行元素进行合并。如下图所示的源数据,
在这里插入图片描述
可以看到,该Excel表格的A列有很多重复项,对他们进行合并,其它列直接连接起来,效果如下图所示。

在这里插入图片描述

解决方案

在这里提供一个可设定的解决方案,也是一个SUB子程序。源代码如下。

Sub mergeCategoryValues()
    Dim lngRow As Long

    With ActiveSheet
        Dim columnToMatch As Integer: columnToMatch = 1       'Indicates the column to Match
        Dim columnToConcatenateDown As Integer: columnToConcatenateDown = 2   'Indicates the up column to Concatenate
        Dim columnToConcatenateUp As Integer: columnToConcatenateUp = 6         'Indicates the down column to Concatenate
        lngRow = .Cells(Rows.Count, columnToMatch).End(xlUp).Row      'Calculate the Rownum of last line
        .Cells(columnToMatch).CurrentRegion.Sort key1:=.Cells(columnToMatch), Header:=xlYes     'Sort the column to match in order to make the same value appear at the same time

        Do
            If .Cells(lngRow, columnToMatch) = .Cells(lngRow - 1, columnToMatch) Then           'If this line equals to next line of this line
                For i = columnToConcatenateDown To columnToConcatenateUp                        'Concatenate column by column
                    .Cells(lngRow - 1, i) = .Cells(lngRow - 1, i) & Chr(10) & .Cells(lngRow, i)
                Next i
                .Rows(lngRow).Delete                                        'Delete the Row which has been copied
            End If
            lngRow = lngRow - 1                                         'From last to first
        Loop Until lngRow = 1                                           'Until to first
    End With
End Sub

mergeCategoryValues主要就是满足了上述的多行合并需求。因为我在网络上所搜索到的程序大都有很多问题,而这个例程没有BUG,而且会有很详细的代码注释,可以帮助你们进行二次开发。中文代码注释如下:

Sub mergeCategoryValues()
    Dim lngRow As Long
    With ActiveSheet
        Dim columnToMatch As Integer: columnToMatch = 1       'Indicates the column to Match 选择匹配列
        Dim columnToConcatenateDown As Integer: columnToConcatenateDown = 2   'Indicates the up column to Concatenate 选择想要连接的左列 
        Dim columnToConcatenateUp As Integer: columnToConcatenateUp = 6         'Indicates the down column to Concatenate 选择想要连接的右列,例如我要匹配第一列,对应相同元素的第二列到第五列进行连接 那么 columnToConcatenateDown = 2, columnToConcatenateUp = 5
        lngRow = .Cells(Rows.Count, columnToMatch).End(xlUp).Row      'Calculate the Rownum of last line 计算指定匹配列的最后一行的行数
        .Cells(columnToMatch).CurrentRegion.Sort key1:=.Cells(columnToMatch), Header:=xlYes     'Sort the column to match in order to make the same value appear at the same time 对匹配列进行排序,使得相同的元素得以同时出现

        Do
            If .Cells(lngRow, columnToMatch) = .Cells(lngRow - 1, columnToMatch) Then           'If this line equals to next line of this line 如果匹配列两个相邻元素相等,那么触发合并程序
                For i = columnToConcatenateDown To columnToConcatenateUp                        'Concatenate column by column 对连接列进行遍历
                    .Cells(lngRow - 1, i) = .Cells(lngRow - 1, i) & Chr(10) & .Cells(lngRow, i)   '进行连接,并使用Chr(10)作为分隔符
                Next i
                .Rows(lngRow).Delete                                        'Delete the Row which has been copied 删除已经被合并的列
            End If
            lngRow = lngRow - 1                                         'From last to first
        Loop Until lngRow = 1                                           'Until to first
    End With
End Sub

希望该方法可以帮到你,有问题评论区见,我很快会回复。

  • 2
    点赞
  • 23
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
您可以使用以下VBA代码来合并某一工作表的A相同内容单元格: ```vba Sub MergeCells() Dim lastRow As Long Dim rng As Range Dim cell As Range ' 设置要操作的工作表 With ThisWorkbook.Worksheets("Sheet1") '将"Sheet1"替换为您要操作的工作表名称 ' 获取A的最后一行 lastRow = .Cells(.Rows.Count, "A").End(xlUp).Row ' 遍历A中的单元格 For Each cell In .Range("A1:A" & lastRow) ' 检查当前单元格与下一个单元格是否相同 If cell.Value = cell.Offset(1, 0).Value Then ' 如果相同,则合并当前单元格和下一个单元格 If rng Is Nothing Then Set rng = cell Else Set rng = Union(rng, cell) End If Else ' 如果不相同,则合并已找到的相同内容单元格,并清空rng变量 If Not rng Is Nothing Then rng.Merge Set rng = Nothing End If End If Next cell End With ' 合并最后一组相同内容单元格(如果有) If Not rng Is Nothing Then rng.Merge End If ' 清除格式设置,以便显示合并后的内容 With ThisWorkbook.Worksheets("Sheet1") .Range("A1:A" & lastRow).HorizontalAlignment = xlCenter .Range("A1:A" & lastRow).VerticalAlignment = xlCenter .Range("A1:A" & lastRow).WrapText = True .Range("A1:A" & lastRow).EntireColumn.AutoFit End With End Sub ``` 请将代码中的"Sheet1"替换为您要操作的工作表名称。运行此宏后,代码将遍历A中的单元格,找到相同内容单元格并进行合并。最后,代码会清除格式设置,使合并后的内容居中、自动换行并调整宽以适应内容。 请注意,此代码仅合并相邻的相同内容单元格。如果要合并A中非相邻的相同内容单元格,您可能需要进行额外的处理。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Volavion

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值