【发布时间】:2017-08-03 21:03:15
【问题描述】:
【问题讨论】:
-
(1) 从第一条记录中读取源名称,在目标名称中搜索。如果未找到,请将名称添加到目标名称。 (2) 在目标名称中查找源名称(它现在肯定会在那里),如果对应的源列是“x”,则将其他列更新为“x”。 (3) 重复下一条记录。 (可能有更简单的方法可以做到这一点,但我很饿,现在没有思考。)
【问题讨论】:
这可能会让你开始:
Sub mergeCategoryValues()
Dim lngRow As Long
With ActiveSheet
Dim columnToMatch As Integer: columnToMatch = 2
Dim columnToConcatenate As Integer: columnToConcatenate = 4
Dim columnToSum As Integer: columnToSum = 5
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
改编自以下代码:Excel VBA - Combine rows with duplicate values in one cell and merge values in other cell
您需要弄清楚的是删除第一列并使变量更具动态性。让我知道这是否有帮助。
【讨论】: