我有一个如下电子表格: 合并重复项
我可以使用此处的脚本合并重复项。
但是,我不知道将列 A 添加到合并列 (K) 中。任何帮助表示赞赏!
谢谢
假设第 1 行是标题行,因此实际数据从第 2 行开始,并且您希望输出从单元格 J2 开始,此代码应该适合您:
Sub tgr()
Dim cllSKU As Collection
Dim SKUCell As Range
Dim rngFound As Range
Dim arrData(1 To 65000, 1 To 2) As Variant
Dim strFirst As String
Dim strJoin As String
Dim DataIndex As Long
Set cllSKU = New Collection
With Range("G3", Cells(Rows.Count, "G").End(xlUp))
On Error Resume Next
For Each SKUCell In .Cells
cllSKU.Add SKUCell.Text, SKUCell.Text
If cllSKU.Count > DataIndex Then
DataIndex = cllSKU.Count
arrData(DataIndex, 1) = SKUCell.Text
arrData(DataIndex, 2) = Cells(SKUCell.Row, "A").Text & " - ("
Set rngFound = .Find(SKUCell.Text, .Cells(.Cells.Count), xlValues, xlWhole)
If Not rngFound Is Nothing Then
strFirst = rngFound.Address
Do
arrData(DataIndex, 2) = arrData(DataIndex, 2) & Cells(rngFound.Row, "H").Text & ","
Set rngFound = .Find(SKUCell.Text, rngFound, xlValues, xlWhole)
Loop While rngFound.Address <> strFirst
End If
arrData(DataIndex, 2) = Left(arrData(DataIndex, 2), Len(arrData(DataIndex, 2)) - 1) & ")"
End If
Next SKUCell
On Error GoTo 0
End With
If DataIndex > 0 Then
Range("J2:K" & Rows.Count).ClearContents
Range("J2:K2").Resize(DataIndex).Value = arrData
End If
Set cllSKU = Nothing
Set SKUCell = Nothing
Set rngFound = Nothing
Erase arrData
End Sub
可以
在没有.VBA 的情况下实现,但无疑比复制代码对您来说工作量更大!:假设您的标题位于第 3 行。
=IF(COLUMN()<COUNTIF($G:$G,$G3)+8,IF($G3=$G4,INDIRECT("$h"&ROW()+COLUMN()-8),""),"")
=H3&","&I3&","&J3&","&K3&","&L3 (extended as required)
=A3&" - "&"("&M3
(替换M
为步骤 3 中列的列引用)并将两者复制下来以适应。 ,,
。=IF(RIGHT(N3,1)=",",LEFT(N3,LEN(N3)-1)&")",N3&")")
=G2=G3
Merged
B2。请注意,这不会为您的问题中显示的 Tops 提供 M 和 XL 之间的空间。