0
我有两列数据。我想找到第一列中的所有相似值,然后将相关第二列中的数值相加。有数百个随机顺序的值。合并重复的条目并将唯一的数字数据加在一起
NickSlash在遇到第一列中的相似值时如何在第二列中组合文本值编写了一些代码。我是一个完整的新手。基本上NickSlash编写的代码非常棒,除了我需要在第二列中添加数值。 NickSlash回答的这个问题可以在这里找到。 Combining duplicate entries with unique data in Excel
NickSlash写的代码如下,我想这样的代码,而不是电子表格中的公式。
Option Explicit
Sub Main()
Dim Source As Worksheet: Set Source = ThisWorkbook.Worksheets("Sheet1")
Dim Destination As Worksheet: Set Destination = ThisWorkbook.Worksheets("Sheet2")
Dim Records As Object: Set Records = CreateObject("Scripting.Dictionary")
Dim Data As Variant
Dim Index As Long
Dim Row As Integer: Row = 1
Data = Source.Range("A1", "B" & Source.Rows(Source.UsedRange.Rows.Count).Row).Value2
For Index = LBound(Data, 1) To UBound(Data, 1)
If Records.Exists(Data(Index, 1)) Then
Destination.Cells(Records(Data(Index, 1)), 2).Value2 = Destination.Cells(Records(Data(Index, 1)), 2).Value2 & ", " & Data(Index, 2)
Else
Records.Add Data(Index, 1), Row
Destination.Cells(Row, 1).Value2 = Data(Index, 1)
Destination.Cells(Row, 2).Value2 = Data(Index, 2)
Row = Row + 1
End If
Next Index
Set Records = Nothing
End Sub