下面是我的源表
Name Sales
---------------------------------
Thomas 100
Jay 200
Thomas 100
Mathew 50
我需要的输出如下
Name Sales
---------------------------------
Thomas 200
Jay 200
Mathew 50
基本上,我有2列可以有重复,我需要聚合基于第一列的第二列。
我目前的代码如下所示。它工作得非常好。运行4500条记录大约需要45秒。我想知道有没有更有效的方法...因为这似乎是一个微不足道的要求。
'Combine duplicate rows and sum values
Dim Rng As Range
Dim LngRow As Long, i As Long
LngLastRow = lRow 'The last row is calculated somewhere above...
'Initializing the first row
i = 1
'Looping until blank cell is encountered in first column
While Not Cells(i, 1).Value = ""
'Initializing range object
Set Rng = Cells(i, 1)
'Looping from last row to specified first row
For LngRow = LngLastRow To (i + 1) Step -1
'Checking whether value in the cell is equal to specified cell
If Cells(LngRow, 1).Value = Rng.Value Then
Rng.Offset(0, 1).Value = Rng.Offset(0, 1).Value + Cells(LngRow, 2).Value
Rows(LngRow).Delete
End If
Next LngRow
i = i + 1
Wend
请注意,这是一个更大的excel应用程序的一部分,因此我肯定需要的解决方案是在Excel VBA。
https://stackoverflow.com/questions/56246305
复制相似问题