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