vba - Excel VBA - 合并一个单元格中具有重复值的行并合并其他单元格中的值

标签 vba excel duplicates

我试图在一列中查找重复值并将第二列的值合并到一行中。我还想对第三列中的值求和。

例如:

A    B    C    D
h    4    w    3
h    4    u    5
h    4    g    7
h    4    f    4
k    9    t    6
k    9    o    6
k    9    p    9
k    9    j    1

会变成

A    B    C        D
k    9    t;o;p;j  22
h    4    w;u;g;f  19

我在第一部分中使用的代码是

 Sub mergeCategoryValues()
Dim lngRow As Long

With ActiveSheet

lngRow = .Cells(65536, 1).End(xlUp).Row

.Cells(1).CurrentRegion.Sort key1:=.Cells(1), Header:=xlYes 
Do

    If .Cells(lngRow, 9) = .Cells(lngRow + 1, 9) Then
        .Cells(lngRow, 11) = .Cells(lngRow, 8) & "; " & .Cells(lngRow + 1, 8)
        .Rows(lngRow +1).Delete
    End If

    lngRow = lngRow - 1

Loop Until lngRow < 2

End With

End Sub

(请原谅缩进)

我遇到的问题是它会找到第一对重复项,但不是全部。所以我得到的结果如下所示:

A    B    C    D
k    9    t;o  12
k    9    p;j  10   
h    4    w;u  8
h    4    g;f  11

想法?

提前谢谢您。

最佳答案

尝试将代码更改为:

Sub mergeCategoryValues()
    Dim lngRow As Long

    With ActiveSheet
        lngRow = .Cells(65536, 1).End(xlUp).Row
        .Cells(1).CurrentRegion.Sort key1:=.Cells(1), Header:=xlYes

        Do
            If .Cells(lngRow, 1) = .Cells(lngRow - 1, 1) Then
                .Cells(lngRow - 1, 3) = .Cells(lngRow - 1, 3) & "; " & .Cells(lngRow, 3)
                .Cells(lngRow - 1, 4) = .Cells(lngRow - 1, 4) + .Cells(lngRow, 4)
                .Rows(lngRow).Delete
            End If

            lngRow = lngRow - 1
        Loop Until lngRow = 1
    End With
End Sub
<小时/>

已测试

enter image description here

<小时/>

编辑

为了更容易地适应不同的列,我在开头添加了变量来指示哪个列执行什么操作。请注意,当前逻辑中未使用第 2 列 (B)。

Sub mergeCategoryValues()
    Dim lngRow As Long

    With ActiveSheet
        Dim columnToMatch As Integer: columnToMatch = 1
        Dim columnToConcatenate As Integer: columnToConcatenate = 3
        Dim columnToSum As Integer: columnToSum = 4

        lngRow = .Cells(65536, columnToMatch).End(xlUp).Row
        .Cells(columnToMatch).CurrentRegion.Sort key1:=.Cells(columnToMatch), Header:=xlYes

        Do
            If .Cells(lngRow, columnToMatch) = .Cells(lngRow - 1, columnToMatch) Then
                .Cells(lngRow - 1, columnToConcatenate) = .Cells(lngRow - 1, columnToConcatenate) & "; " & .Cells(lngRow, columnToConcatenate)
                .Cells(lngRow - 1, columnToSum) = .Cells(lngRow - 1, columnToSum) + .Cells(lngRow, columnToSum)
                .Rows(lngRow).Delete
            End If

            lngRow = lngRow - 1
        Loop Until lngRow = 1
    End With
End Sub

关于vba - Excel VBA - 合并一个单元格中具有重复值的行并合并其他单元格中的值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20227196/

相关文章:

html - 如何点击没有ID的网页元素按钮?

objective-c - objective-c 是否遵循操作顺序(Bedmas)?

vba - 如何在 Excel VBA 用户窗体中创建两列查找组合框

HTML/CSS 有没有办法复制这个按钮

python - 使用字典附加多个文件并删除重复项

excel - VBA 用字符串中的另一个对象修剪一个对象(FSO.Getfolder 相关)基本上最终得到文件的相对路径

javascript - VBA 中的正则表达式 - 如何恢复搜索?

php - Insert Into on Duplicate Update 给我创建了一个不需要的行

excel - 在Excel中计算矛兵排名统计的p值(使用或不使用vb​​a)

JAVA读取excel文件