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

时间:2021-06-05 07:39:02

I am trying to find duplicate values in one column and combine the values of a second column into one row. I also want to sum the values in a third column.

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

For example:

例如:

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

Would become

将成为

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

The code I have been using for the first part of this is

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

 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

(please forgive the indentation)

(请原谅缩进)

The problem that I am running into is that it will find the first pair of duplicates, but not all. So I get a result that looks like this:

我遇到的问题是,它会找到第一对副本,但不是全部。结果是这样的

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

Thoughts?

想法吗?

Thank you in advance.

提前谢谢你。

6 个解决方案

#1


7  

Try changing your code to this:

试着改变你的代码:

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

Tested

测试

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


EDIT

编辑

To make it a little easier to adjust to different column I added variables at the beginning to indicate which column do what. Note that column 2 (B) isn't used in the current logic.

为了更容易地调整到不同的列,我在开始时添加了变量,以指示哪个列做什么。注意,列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

#2


1  

This looks sloppy and complicated. Both are true, but it works pretty fine. Note! I always recommend to define all DIMs like: ranges, integers, etc. Storing the last row to a variable like LngRow is best (not like the whole App.WksFunc.COUNTA). I also like to use functions directly on cells where possible (like the SUMIFS ex. below). Thus, based on your example configuration (columns ABCD):

这看起来草率而复杂。两者都是正确的,但都很好。注意!我总是建议定义所有的DIMs,比如:范围、整数等等。把最后一行存储到一个像LngRow这样的变量中是最好的(不像整个App.WksFunc.COUNTA)。我还喜欢在可能的地方直接使用函数(比如下面的SUMIFS ex.)。因此,基于您的示例配置(列ABCD):

Sub Test_Texas2014()
Dim MySheet As Worksheet: Set MySheet = Sheets("Sheet1")

'Clear the previous results before populating 
MySheet.Range("F:I").Clear

'Step1 Find distinct values on column A and copy them on F
For i = 1 To Application.WorksheetFunction.CountA(MySheet.Range("A:A"))
    Row_PasteCount = Application.WorksheetFunction.CountA(MySheet.Range("F:F")) + 1
    Set LookupID = MySheet.Range("A" & i)
    Set LookupID_SearchRange = MySheet.Range("F:F")
    Set CopyValueID_Paste = MySheet.Range("F" & Row_PasteCount)
        If IsError(Application.Match(LookupID, LookupID_SearchRange, 0)) Then
            LookupID.Copy
            CopyValueID_Paste.PasteSpecial xlPasteValues
        End If
Next i

'Step2 fill your values in columns G H I based on selection
For j = 1 To Application.WorksheetFunction.CountA(MySheet.Range("F:F"))
    Set ID = MySheet.Range("F" & j)
    Set Index = MySheet.Range("G" & j)
    Set AttributeX = MySheet.Range("H" & j)
    Set SumX = MySheet.Range("I" & j)
    For k = 1 To Application.WorksheetFunction.CountA(MySheet.Range("A:A"))
        Set SearchedID = MySheet.Range("A" & k)
        Set SearchedID_Index = MySheet.Range("B" & k)
        Set SearchedID_AttributeX = MySheet.Range("C" & k)
        Set SearchedID_SumX = MySheet.Range("D" & k)
            If ID.Value = SearchedID.Value Then
                Index.Value = SearchedID_Index.Value
                AttributeX.Value = AttributeX.Value & ";" & SearchedID_AttributeX.Value
                SumX.Value = SumX.Value + SearchedID_SumX.Value
            End If
        Next k
    Next j
End Sub

'Although for the sum I would use something like:
MySheet.Range("I1").Formula = "=SUMIFS(D:D,A:A,F1)"
MySheet.Range("I1").Copy
MySheet.Range("I2:I" & Application.WorksheetFunction.CountA(MySheet.Range("I:I"))).pasteSpecial xlPasteFormulas
'Similar for the Index with a Vlookup or Index(Match())

#3


1  

Merging rows by summing the numbers from column D and building a string concatenation from column C with a semi-colon delimiter based upon duplicate values in columns A and B.

通过将数字与列D相加,并在列C中构建一个字符串连接,并根据列a和B中的重复值建立一个分号分隔符。

Before¹:

¹之前:

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

        

Code:

代码:

Sub merge_A_to_D_data()
    Dim rw As Long, lr As Long, str As String, dbl As Double

    Application.ScreenUpdating = False
    With ActiveSheet.Cells(1, 1).CurrentRegion
        .Cells.Sort Key1:=.Columns(1), Order1:=xlAscending, _
                    Key2:=.Columns(2), Order2:=xlAscending, _
                    Orientation:=xlTopToBottom, Header:=xlYes
        lr = .Rows.Count
        For rw = .Rows.Count To 2 Step -1
            If .Cells(rw, 1).Value2 <> .Cells(rw - 1, 1).Value2 And _
               .Cells(rw, 2).Value2 <> .Cells(rw - 1, 2).Value2 And rw < lr Then
                .Cells(rw, 4) = Application.Sum(.Range(.Cells(rw, 4), .Cells(lr, 4)))
                .Cells(rw, 3) = Join(Application.Transpose(.Range(.Cells(rw, 3), .Cells(lr, 3))), Chr(59))
                .Cells(rw + 1, 1).Resize(lr - rw, 1).EntireRow.Delete
                lr = rw - 1
            End If
        Next rw
    End With
    Application.ScreenUpdating = True
End Sub

After¹:

¹之后:

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

        

¹Some additional rows of data were added to the original posted data in order to demonstrate the sort.

¹一些额外的行数据被添加到原始发布数据,以证明。

#4


1  

Here is my solution

这是我的解决方案

Sub MyCombine()
Dim i As Integer
ActiveSheet.Sort.SortFields.Add Key:=Range("A:A"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.Sort
    .SetRange Range("A:D")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlStroke
    .Apply
End With

i = 2

Do Until Len(Cells(i, 1).Value) = 0
    If Cells(i, 1).Value = Cells(i + 1, 1).Value Then
        Cells(i, 3).Value = Cells(i, 3).Value & ";" & Cells(i + 1, 3).Value
        Cells(i, 4).Value = Cells(i, 4).Value + Cells(i + 1, 4).Value
        Rows(i + 1).Delete
    Else
        i = i + 1
    End If
Loop    
End Sub

#5


0  

.Cells(lngRow, 11) = .Cells(lngRow, 8) & "; " & .Cells(lngRow + 1, 8)

.细胞(lngRow, 11) = .Cells(lngRow, 8) &;" & .Cells(lngRow + 1,8)

should be

应该是

.Cells(lngRow, 11) = .Cells(lngRow, 8) & "; " & .Cells(lngRow + 1, 11)

.细胞(lngRow, 11) = .Cells(lngRow, 8) &;& .细胞(lngRow + 1,11)

#6


0  

This will do what you want.

这将做你想做的。

Sub Macro()
Dim lngRow As Long
For lngRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
If StrComp(Range("B" & lngRow), Range("B" & lngRow - 1), vbTextCompare) = 0 Then
If Range("C" & lngRow) <> "" Then
Range("C" & lngRow - 1) = Range("C" & lngRow - 1) & ";" & Range("C" & lngRow)
Range("D" & lngRow - 1) = Range("D" & lngRow - 1) + Range("D" & lngRow)
End If
Rows(lngRow).Delete
End If
Next
End Sub

#1


7  

Try changing your code to this:

试着改变你的代码:

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

Tested

测试

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


EDIT

编辑

To make it a little easier to adjust to different column I added variables at the beginning to indicate which column do what. Note that column 2 (B) isn't used in the current logic.

为了更容易地调整到不同的列,我在开始时添加了变量,以指示哪个列做什么。注意,列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

#2


1  

This looks sloppy and complicated. Both are true, but it works pretty fine. Note! I always recommend to define all DIMs like: ranges, integers, etc. Storing the last row to a variable like LngRow is best (not like the whole App.WksFunc.COUNTA). I also like to use functions directly on cells where possible (like the SUMIFS ex. below). Thus, based on your example configuration (columns ABCD):

这看起来草率而复杂。两者都是正确的,但都很好。注意!我总是建议定义所有的DIMs,比如:范围、整数等等。把最后一行存储到一个像LngRow这样的变量中是最好的(不像整个App.WksFunc.COUNTA)。我还喜欢在可能的地方直接使用函数(比如下面的SUMIFS ex.)。因此,基于您的示例配置(列ABCD):

Sub Test_Texas2014()
Dim MySheet As Worksheet: Set MySheet = Sheets("Sheet1")

'Clear the previous results before populating 
MySheet.Range("F:I").Clear

'Step1 Find distinct values on column A and copy them on F
For i = 1 To Application.WorksheetFunction.CountA(MySheet.Range("A:A"))
    Row_PasteCount = Application.WorksheetFunction.CountA(MySheet.Range("F:F")) + 1
    Set LookupID = MySheet.Range("A" & i)
    Set LookupID_SearchRange = MySheet.Range("F:F")
    Set CopyValueID_Paste = MySheet.Range("F" & Row_PasteCount)
        If IsError(Application.Match(LookupID, LookupID_SearchRange, 0)) Then
            LookupID.Copy
            CopyValueID_Paste.PasteSpecial xlPasteValues
        End If
Next i

'Step2 fill your values in columns G H I based on selection
For j = 1 To Application.WorksheetFunction.CountA(MySheet.Range("F:F"))
    Set ID = MySheet.Range("F" & j)
    Set Index = MySheet.Range("G" & j)
    Set AttributeX = MySheet.Range("H" & j)
    Set SumX = MySheet.Range("I" & j)
    For k = 1 To Application.WorksheetFunction.CountA(MySheet.Range("A:A"))
        Set SearchedID = MySheet.Range("A" & k)
        Set SearchedID_Index = MySheet.Range("B" & k)
        Set SearchedID_AttributeX = MySheet.Range("C" & k)
        Set SearchedID_SumX = MySheet.Range("D" & k)
            If ID.Value = SearchedID.Value Then
                Index.Value = SearchedID_Index.Value
                AttributeX.Value = AttributeX.Value & ";" & SearchedID_AttributeX.Value
                SumX.Value = SumX.Value + SearchedID_SumX.Value
            End If
        Next k
    Next j
End Sub

'Although for the sum I would use something like:
MySheet.Range("I1").Formula = "=SUMIFS(D:D,A:A,F1)"
MySheet.Range("I1").Copy
MySheet.Range("I2:I" & Application.WorksheetFunction.CountA(MySheet.Range("I:I"))).pasteSpecial xlPasteFormulas
'Similar for the Index with a Vlookup or Index(Match())

#3


1  

Merging rows by summing the numbers from column D and building a string concatenation from column C with a semi-colon delimiter based upon duplicate values in columns A and B.

通过将数字与列D相加,并在列C中构建一个字符串连接,并根据列a和B中的重复值建立一个分号分隔符。

Before¹:

¹之前:

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

        

Code:

代码:

Sub merge_A_to_D_data()
    Dim rw As Long, lr As Long, str As String, dbl As Double

    Application.ScreenUpdating = False
    With ActiveSheet.Cells(1, 1).CurrentRegion
        .Cells.Sort Key1:=.Columns(1), Order1:=xlAscending, _
                    Key2:=.Columns(2), Order2:=xlAscending, _
                    Orientation:=xlTopToBottom, Header:=xlYes
        lr = .Rows.Count
        For rw = .Rows.Count To 2 Step -1
            If .Cells(rw, 1).Value2 <> .Cells(rw - 1, 1).Value2 And _
               .Cells(rw, 2).Value2 <> .Cells(rw - 1, 2).Value2 And rw < lr Then
                .Cells(rw, 4) = Application.Sum(.Range(.Cells(rw, 4), .Cells(lr, 4)))
                .Cells(rw, 3) = Join(Application.Transpose(.Range(.Cells(rw, 3), .Cells(lr, 3))), Chr(59))
                .Cells(rw + 1, 1).Resize(lr - rw, 1).EntireRow.Delete
                lr = rw - 1
            End If
        Next rw
    End With
    Application.ScreenUpdating = True
End Sub

After¹:

¹之后:

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

        

¹Some additional rows of data were added to the original posted data in order to demonstrate the sort.

¹一些额外的行数据被添加到原始发布数据,以证明。

#4


1  

Here is my solution

这是我的解决方案

Sub MyCombine()
Dim i As Integer
ActiveSheet.Sort.SortFields.Add Key:=Range("A:A"), _
SortOn:=xlSortOnValues, Order:=xlAscending, DataOption:=xlSortNormal
With ActiveSheet.Sort
    .SetRange Range("A:D")
    .Header = xlYes
    .MatchCase = False
    .Orientation = xlTopToBottom
    .SortMethod = xlStroke
    .Apply
End With

i = 2

Do Until Len(Cells(i, 1).Value) = 0
    If Cells(i, 1).Value = Cells(i + 1, 1).Value Then
        Cells(i, 3).Value = Cells(i, 3).Value & ";" & Cells(i + 1, 3).Value
        Cells(i, 4).Value = Cells(i, 4).Value + Cells(i + 1, 4).Value
        Rows(i + 1).Delete
    Else
        i = i + 1
    End If
Loop    
End Sub

#5


0  

.Cells(lngRow, 11) = .Cells(lngRow, 8) & "; " & .Cells(lngRow + 1, 8)

.细胞(lngRow, 11) = .Cells(lngRow, 8) &;" & .Cells(lngRow + 1,8)

should be

应该是

.Cells(lngRow, 11) = .Cells(lngRow, 8) & "; " & .Cells(lngRow + 1, 11)

.细胞(lngRow, 11) = .Cells(lngRow, 8) &;& .细胞(lngRow + 1,11)

#6


0  

This will do what you want.

这将做你想做的。

Sub Macro()
Dim lngRow As Long
For lngRow = ActiveSheet.Cells(Rows.Count, "A").End(xlUp).Row To 2 Step -1
If StrComp(Range("B" & lngRow), Range("B" & lngRow - 1), vbTextCompare) = 0 Then
If Range("C" & lngRow) <> "" Then
Range("C" & lngRow - 1) = Range("C" & lngRow - 1) & ";" & Range("C" & lngRow)
Range("D" & lngRow - 1) = Range("D" & lngRow - 1) + Range("D" & lngRow)
End If
Rows(lngRow).Delete
End If
Next
End Sub